using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace PRSDesktop
{
///
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
///
public class ScreenCapture
{
///
/// Creates an Image object containing a screen shot of the entire desktop
///
///
public Bitmap CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
///
/// Creates an Image object containing a screen shot of a section of the desktop
///
///
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);
}
///
/// Creates an Image object containing a screen shot of a specific window
///
/// The handle to the window. (In windows forms, this is obtained by the Handle property)
///
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;
}
///
/// Helper class containing Gdi32 API functions
///
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);
}
///
/// Helper class containing User32 API functions
///
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;
}
}
}
}