前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Winform屏幕截图保存C#代码

Winform屏幕截图保存C#代码

作者头像
全栈程序员站长
发布2022-07-15 14:30:43
6120
发布2022-07-15 14:30:43
举报

大家好,又见面了,我是全栈君

代码如下:

代码语言:javascript
复制
 1 using System.Runtime.InteropServices;
 2 using System.Drawing.Imaging;
 3 
 4  
 5 
 6     [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
 7     private static extern bool BitBlt(
 8 
 9                     IntPtr hdcDest,   //   目标   DC的句柄   
10                     int nXDest,
11                     int nYDest,
12                     int nWidth,
13                     int nHeight,
14                     IntPtr hdcSrc,     //   源DC的句柄   
15                     int nXSrc,
16                     int nYSrc,
17                     System.Int32 dwRop     //   光栅的处理数值   
18                       ); 
19 
20  
21 
22   private   void   button1_Click(object   sender,   System.EventArgs   e)   
23   {   
24   //获得当前屏幕的大小   http://www.cnblogs.com/roucheng/
25   Rectangle   rect   =   new   Rectangle   (   )   ;   
26   rect   =   Screen.GetWorkingArea   (   this   )   ;   
27   //创建一个以当前屏幕为模板的图象   
28   Graphics   g1   =   this.CreateGraphics   (   )   ;   
29   //创建以屏幕大小为标准的位图     
30   Image   MyImage   =   new   Bitmap   (   rect.Width   ,   rect.Height   ,   g1   )   ;   
31   Graphics   g2   =   Graphics.FromImage   (   MyImage   )   ;   
32   //得到屏幕的DC   
33   IntPtr   dc1   =   g1.GetHdc   (   )   ;   
34   //得到Bitmap的DC     
35   IntPtr   dc2   =   g2.GetHdc   (   )   ;   
36   //调用此API函数,实现屏幕捕获   
37   BitBlt   (   dc2   ,   0   ,   0   ,   rect.Width   ,   rect.Height   ,   dc1   ,   0   ,   0   ,   13369376   )   ;   
38   //释放掉屏幕的DC   
39   g1.ReleaseHdc   (   dc1   )   ;   
40   //释放掉Bitmap的DC     
41   g2.ReleaseHdc   (   dc2   )   ;   
42   //以JPG文件格式来保存   
43   MyImage.Save   (   @"c:/Capture.jpg"   ,   ImageFormat.Jpeg   );   
44   MessageBox.Show   (   "当前屏幕已经保存为C盘的capture.jpg文件!"   )   ;   
45   }

已在项目中实现:http://hovertree.com/h/bjaf/76q5yeli.htm

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120703.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档