c#截取后台窗口的图片

c#截取后台窗口的图片,自测可用,据说性能很一般,用用吧

 1         struct RECT
 2         {
 3             public int Left; // x position of upper-left corner
 4             public int Top; // y position of upper-left corner
 5             public int Right; // x position of lower-right corner
 6             public int Bottom; // y position of lower-right corner
 7         }
 8         public static Bitmap GetWindow(IntPtr hWnd)
 9         {
10             IntPtr hscrdc = GetWindowDC(hWnd);
11             RECT rect = new RECT();
12             GetWindowRect(hWnd, out rect);
13             IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, rect.Right - rect.Left, rect.Bottom - rect.Top);
14             IntPtr hmemdc = CreateCompatibleDC(hscrdc);
15             SelectObject(hmemdc, hbitmap);
16             PrintWindow(hWnd, hmemdc, 0);
17             Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
18             DeleteDC(hscrdc); 
19             DeleteDC(hmemdc);
20             return bmp;
21         }
22 
23         //API声明
24 
25         [DllImport("user32.dll")]
26         private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
27 
28         [DllImport("gdi32.dll")]
29         public static extern IntPtr CreateDC(
30                     string lpszDriver,        // driver name驱动名  
31                     string lpszDevice,        // device name设备名  
32                     string lpszOutput,        // not used; should be NULL  
33                     IntPtr lpInitData  // optional printer data  
34          );
35 
36         [DllImport("gdi32.dll")]
37         public static extern int BitBlt(
38          IntPtr hdcDest, // handle to destination DC目标设备的句柄  
39          int nXDest,  // x-coord of destination upper-left corner目标对象的左上角的X坐标  
40          int nYDest,  // y-coord of destination upper-left corner目标对象的左上角的Y坐标  
41          int nWidth,  // width of destination rectangle目标对象的矩形宽度  
42          int nHeight, // height of destination rectangle目标对象的矩形长度  
43          IntPtr hdcSrc,  // handle to source DC源设备的句柄  
44          int nXSrc,   // x-coordinate of source upper-left corner源对象的左上角的X坐标  
45          int nYSrc,   // y-coordinate of source upper-left corner源对象的左上角的Y坐标  
46          UInt32 dwRop  // raster operation code光栅的操作值  
47          );
48 
49         [DllImport("gdi32.dll")]
50         public static extern IntPtr CreateCompatibleDC(
51          IntPtr hdc // handle to DC  
52          );
53 
54         [DllImport("gdi32.dll")]
55         public static extern IntPtr CreateCompatibleBitmap(
56          IntPtr hdc,        // handle to DC  
57          int nWidth,     // width of bitmap, in pixels  
58          int nHeight     // height of bitmap, in pixels  
59          );
60 
61         [DllImport("gdi32.dll")]
62         public static extern IntPtr SelectObject(
63          IntPtr hdc,          // handle to DC  
64          IntPtr hgdiobj   // handle to object  
65          );
66 
67         [DllImport("gdi32.dll")]
68         public static extern int DeleteDC(
69          IntPtr hdc          // handle to DC  
70          );
71 
72         [DllImport("user32.dll")]
73         public static extern bool PrintWindow(
74          IntPtr hwnd,               // Window to copy,Handle to the window that will be copied.   
75          IntPtr hdcBlt,             // HDC to print into,Handle to the device context.   
76          UInt32 nFlags              // Optional flags,Specifies the drawing options. It can be one of the following values.   
77          );
78 
79         [DllImport("user32.dll")]
80         public static extern IntPtr GetWindowDC(
81          IntPtr hwnd
82          );