ScreenShot.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. namespace PRSDesktop
  5. {
  6. /// <summary>
  7. /// Provides functions to capture the entire screen, or a particular window, and save it to a file.
  8. /// </summary>
  9. public class ScreenCapture
  10. {
  11. /// <summary>
  12. /// Creates an Image object containing a screen shot of the entire desktop
  13. /// </summary>
  14. /// <returns></returns>
  15. public Bitmap CaptureScreen()
  16. {
  17. return CaptureWindow(User32.GetDesktopWindow());
  18. }
  19. /// <summary>
  20. /// Creates an Image object containing a screen shot of a section of the desktop
  21. /// </summary>
  22. /// <returns></returns>
  23. public Bitmap CaptureScreen(int x, int y, int w, int h)
  24. {
  25. return CaptureWindow(User32.GetDesktopWindow(), x, y, w, h);
  26. }
  27. public Bitmap CaptureWindow(IntPtr handle)
  28. {
  29. var windowRect = new User32.RECT();
  30. User32.GetWindowRect(handle, ref windowRect);
  31. var width = windowRect.right - windowRect.left;
  32. var height = windowRect.bottom - windowRect.top;
  33. return CaptureWindow(handle, 0, 0, width, height);
  34. }
  35. /// <summary>
  36. /// Creates an Image object containing a screen shot of a specific window
  37. /// </summary>
  38. /// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
  39. /// <returns></returns>
  40. public Bitmap CaptureWindow(IntPtr handle, int x, int y, int w, int h)
  41. {
  42. // get te hDC of the target window
  43. var hdcSrc = User32.GetWindowDC(handle);
  44. // get the size
  45. var windowRect = new User32.RECT { left = x, top = y, right = x + w, bottom = y + h };
  46. //User32.GetWindowRect(handle, ref windowRect);
  47. //int width = windowRect.right - windowRect.left;
  48. //int height = windowRect.bottom - windowRect.top;
  49. // create a device context we can copy to
  50. var hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
  51. // create a bitmap we can copy it to,
  52. // using GetDeviceCaps to get the width/height
  53. var hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, w, h);
  54. // select the bitmap object
  55. var hOld = GDI32.SelectObject(hdcDest, hBitmap);
  56. // bitblt over
  57. GDI32.BitBlt(hdcDest, 0, 0, w, h, hdcSrc, x, y, GDI32.SRCCOPY);
  58. // restore selection
  59. GDI32.SelectObject(hdcDest, hOld);
  60. // clean up
  61. GDI32.DeleteDC(hdcDest);
  62. User32.ReleaseDC(handle, hdcSrc);
  63. // get a .NET image object for it
  64. var img = Image.FromHbitmap(hBitmap);
  65. // free up the Bitmap object
  66. GDI32.DeleteObject(hBitmap);
  67. return img;
  68. }
  69. /// <summary>
  70. /// Helper class containing Gdi32 API functions
  71. /// </summary>
  72. private class GDI32
  73. {
  74. public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
  75. [DllImport("gdi32.dll")]
  76. public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
  77. int nWidth, int nHeight, IntPtr hObjectSource,
  78. int nXSrc, int nYSrc, int dwRop);
  79. [DllImport("gdi32.dll")]
  80. public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
  81. int nHeight);
  82. [DllImport("gdi32.dll")]
  83. public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  84. [DllImport("gdi32.dll")]
  85. public static extern bool DeleteDC(IntPtr hDC);
  86. [DllImport("gdi32.dll")]
  87. public static extern bool DeleteObject(IntPtr hObject);
  88. [DllImport("gdi32.dll")]
  89. public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  90. }
  91. /// <summary>
  92. /// Helper class containing User32 API functions
  93. /// </summary>
  94. private class User32
  95. {
  96. [DllImport("user32.dll")]
  97. public static extern IntPtr GetDesktopWindow();
  98. [DllImport("user32.dll")]
  99. public static extern IntPtr GetWindowDC(IntPtr hWnd);
  100. [DllImport("user32.dll")]
  101. public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
  102. [DllImport("user32.dll")]
  103. public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
  104. [StructLayout(LayoutKind.Sequential)]
  105. public struct RECT
  106. {
  107. public int left;
  108. public int top;
  109. public int right;
  110. public int bottom;
  111. }
  112. }
  113. }
  114. }