Winform屏幕截图保存C#代码

代码如下:

 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://www.cnblogs.com/roucheng/p/3551023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值