123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Drawing;
- using System.Runtime.InteropServices;
- namespace PRSDesktop
- {
- /// <summary>
- /// Provides functions to capture the entire screen, or a particular window, and save it to a file.
- /// </summary>
- public class ScreenCapture
- {
- /// <summary>
- /// Creates an Image object containing a screen shot of the entire desktop
- /// </summary>
- /// <returns></returns>
- public Bitmap CaptureScreen()
- {
- return CaptureWindow(User32.GetDesktopWindow());
- }
- /// <summary>
- /// Creates an Image object containing a screen shot of a section of the desktop
- /// </summary>
- /// <returns></returns>
- public Bitmap CaptureScreen(int x, int y, int w, int h)
- {
- return CaptureWindow(User32.GetDesktopWindow(), x, y, w, h);
- }
- public Bitmap CaptureWindow(IntPtr handle)
- {
- var windowRect = new User32.RECT();
- User32.GetWindowRect(handle, ref windowRect);
- var width = windowRect.right - windowRect.left;
- var height = windowRect.bottom - windowRect.top;
- return CaptureWindow(handle, 0, 0, width, height);
- }
- /// <summary>
- /// Creates an Image object containing a screen shot of a specific window
- /// </summary>
- /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
- /// <returns></returns>
- public Bitmap CaptureWindow(IntPtr handle, int x, int y, int w, int h)
- {
- // get te hDC of the target window
- var hdcSrc = User32.GetWindowDC(handle);
- // get the size
- var windowRect = new User32.RECT { left = x, top = y, right = x + w, bottom = y + h };
- //User32.GetWindowRect(handle, ref windowRect);
- //int width = windowRect.right - windowRect.left;
- //int height = windowRect.bottom - windowRect.top;
- // create a device context we can copy to
- var hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
- // create a bitmap we can copy it to,
- // using GetDeviceCaps to get the width/height
- var hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, w, h);
- // select the bitmap object
- var hOld = GDI32.SelectObject(hdcDest, hBitmap);
- // bitblt over
- GDI32.BitBlt(hdcDest, 0, 0, w, h, hdcSrc, x, y, GDI32.SRCCOPY);
- // restore selection
- GDI32.SelectObject(hdcDest, hOld);
- // clean up
- GDI32.DeleteDC(hdcDest);
- User32.ReleaseDC(handle, hdcSrc);
- // get a .NET image object for it
- var img = Image.FromHbitmap(hBitmap);
- // free up the Bitmap object
- GDI32.DeleteObject(hBitmap);
- return img;
- }
- /// <summary>
- /// Helper class containing Gdi32 API functions
- /// </summary>
- private class GDI32
- {
- public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
- [DllImport("gdi32.dll")]
- public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
- int nWidth, int nHeight, IntPtr hObjectSource,
- int nXSrc, int nYSrc, int dwRop);
- [DllImport("gdi32.dll")]
- public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
- int nHeight);
- [DllImport("gdi32.dll")]
- public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
- [DllImport("gdi32.dll")]
- public static extern bool DeleteDC(IntPtr hDC);
- [DllImport("gdi32.dll")]
- public static extern bool DeleteObject(IntPtr hObject);
- [DllImport("gdi32.dll")]
- public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
- }
- /// <summary>
- /// Helper class containing User32 API functions
- /// </summary>
- private class User32
- {
- [DllImport("user32.dll")]
- public static extern IntPtr GetDesktopWindow();
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindowDC(IntPtr hWnd);
- [DllImport("user32.dll")]
- public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int left;
- public int top;
- public int right;
- public int bottom;
- }
- }
- }
- }
|