///
<summary>
/// 截取屏幕 /// </summary> class ScreenGrab { #region Functional imports for ScreenGrab functionality [DllImport( " GDI32.dll " )] public static extern bool BitBlt( int hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, int hdcSrc, int nXSrc, int nYSrc, int dwRop); [DllImport( " GDI32.dll " )] public static extern int CreateCompatibleBitmap( int hdc, int nWidth, int nHeight); [DllImport( " GDI32.dll " )] public static extern int CreateCompatibleDC( int hdc); [DllImport( " GDI32.dll " )] public static extern bool DeleteDC( int hdc); [DllImport( " GDI32.dll " )] public static extern bool DeleteObject( int hObject); [DllImport( " GDI32.dll " )] public static extern int GetDeviceCaps( int hdc, int nIndex); [DllImport( " GDI32.dll " )] public static extern int SelectObject( int hdc, int hgdiobj); [DllImport( " User32.dll " )] public static extern int GetDesktopWindow(); [DllImport( " User32.dll " )] public static extern int GetWindowDC( int hWnd); [DllImport( " User32.dll " )] public static extern int ReleaseDC( int hWnd, int hDC); #endregion // Captures the current on-screen representation using Windows API calls public Bitmap CaptureScreen() { // Provides a pointer to the visual representation of the desktop window int source = GetWindowDC(GetDesktopWindow()); // Secures the image using CreateCompatibleBitmap int bitmap = CreateCompatibleBitmap(source, GetDeviceCaps(source, 8 ), GetDeviceCaps(source, 10 )); int destination = CreateCompatibleDC(source); SelectObject(destination, bitmap); BitBlt(destination, 0 , 0 , GetDeviceCaps(source, 8 ), GetDeviceCaps(source, 10 ), source, 0 , 0 , 0x00CC0020 ); Bitmap image = GetImage(bitmap); Cleanup(bitmap, source, destination); return image; } private void Cleanup( int bitmap, int source, int destination) { ReleaseDC(GetDesktopWindow(), source); DeleteDC(destination); DeleteObject(bitmap); } private Bitmap GetImage( int hBitmap) { Bitmap image = new Bitmap(Image.FromHbitmap( new IntPtr(hBitmap)), Image.FromHbitmap( new IntPtr(hBitmap)).Width, Image.FromHbitmap( new IntPtr(hBitmap)).Height); return image; } } |
转载于:https://www.cnblogs.com/EasyData/archive/2008/09/05/1285339.html