//http://cherishlc.iteye.com/blog/1687738
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Linq;
  5 using System.Runtime.InteropServices;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using System.Windows.Forms;
  9 
 10 namespace LC
 11 {
 12     class ScreenCapture
 13     {
 14         #region 抓取屏幕
 15         /// <summary>
 16         /// 抓取屏幕(层叠的窗口)
 17         /// </summary>
 18         /// <param name="x">左上角的横坐标</param>
 19         /// <param name="y">左上角的纵坐标</param>
 20         /// <param name="width">抓取宽度</param>
 21         /// <param name="height">抓取高度</param>
 22         /// <returns></returns>
 23         public static Bitmap captureScreen(int x, int y, int width, int height)
 24         {
 25             Bitmap bmp = new Bitmap(width, height);
 26             using (Graphics g = Graphics.FromImage(bmp))
 27             {
 28                 g.CopyFromScreen(new Point(x, y), new Point(0, 0), bmp.Size);
 29                 g.Dispose();
 30             }
 31             //bit.Save(@"capture2.png");
 32             return bmp;
 33         }
 34 
 35         /// <summary>
 36         ///  抓取整个屏幕
 37         /// </summary>
 38         /// <returns></returns>
 39         public static Bitmap captureScreen()
 40         {
 41             Size screenSize = Screen.PrimaryScreen.Bounds.Size;
 42             return captureScreen(0,0,screenSize.Width,screenSize.Height);
 43         }
 44         #endregion
 45 
 46         #region 使用BitBlt方法抓取控件,无论控件是否被遮挡
 47         /// <summary>
 48         /// 控件(窗口)的截图,控件被其他窗口(而非本窗口内控件)遮挡时也可以正确截图,使用BitBlt方法
 49         /// </summary>
 50         /// <param name="control">需要被截图的控件</param>
 51         /// <returns>该控件的截图,控件被遮挡时也可以正确截图</returns>
 52         public static Bitmap captureControl(Control control)
 53         {
 54             //调用API截屏
 55             IntPtr hSrce = GetWindowDC(control.Handle);
 56             IntPtr hDest = CreateCompatibleDC(hSrce);
 57             IntPtr hBmp = CreateCompatibleBitmap(hSrce, control.Width, control.Height);
 58             IntPtr hOldBmp = SelectObject(hDest, hBmp);
 59             if (BitBlt(hDest, 0, 0, control.Width, control.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt))
 60             {
 61                 Bitmap bmp = Image.FromHbitmap(hBmp);
 62                 SelectObject(hDest, hOldBmp);
 63                 DeleteObject(hBmp);
 64                 DeleteDC(hDest);
 65                 ReleaseDC(control.Handle, hSrce);
 66                 // bmp.Save(@"a.png");
 67                 // bmp.Dispose();
 68                 return bmp;
 69             }
 70             return null;
 71 
 72         }
 73 
 74         #endregion
 75 
 76 
 77         #region 使用PrintWindow方法抓取窗口,无论控件是否被遮挡
 78         /// <summary>
 79         /// 窗口的截图,窗口被遮挡时也可以正确截图,使用PrintWindow方法
 80         /// </summary>
 81         /// <param name="control">需要被截图的窗口</param>
 82         /// <returns>窗口的截图,控件被遮挡时也可以正确截图</returns>
 83         public static Bitmap captureWindowUsingPrintWindow(Form form)
 84         {
 85             return GetWindow(form.Handle);
 86         }
 87 
 88 
 89         private static Bitmap GetWindow(IntPtr hWnd)
 90         {
 91             IntPtr hscrdc = GetWindowDC(hWnd);
 92             Control control = Control.FromHandle(hWnd);
 93             IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
 94             IntPtr hmemdc = CreateCompatibleDC(hscrdc);
 95             SelectObject(hmemdc, hbitmap);
 96             PrintWindow(hWnd, hmemdc, 0);
 97             Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
 98             DeleteDC(hscrdc);//删除用过的对象
 99             DeleteDC(hmemdc);//删除用过的对象
100             return bmp;
101         }
102         #endregion
103 
104         #region  DLL calls
105         [DllImport("gdi32.dll")]
106         static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int
107         wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
108         [DllImport("gdi32.dll")]
109         static extern IntPtr DeleteDC(IntPtr hDc);
110         [DllImport("gdi32.dll")]
111         static extern IntPtr DeleteObject(IntPtr hDc);
112         [DllImport("gdi32.dll")]
113         static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
114         [DllImport("gdi32.dll")]
115         static extern IntPtr CreateCompatibleDC(IntPtr hdc);
116         [DllImport("gdi32.dll")]
117         static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
118         [DllImport("user32.dll")]
119         public static extern IntPtr GetDesktopWindow();
120         [DllImport("user32.dll")]
121         public static extern IntPtr GetWindowDC(IntPtr ptr);
122         [DllImport("user32.dll")]
123         public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
124         [DllImport("user32.dll")]
125         static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
126         #endregion
127     }
128 }