日期:2014-06-10 浏览次数:20715 次
代码如下:
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 }