Advertisement
Guest User

Untitled

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