ウインドウ画面のキャプチャ
Windows APIのPrintWindow()を使用してウインドウ画面をキャプチャします。ウインドウが隠れている場合でも画像の取得が出来ます。
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[System.Runtime.InteropServices.DllImport("User32.dll")]
private extern static bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
private void Button_Click(object sender, RoutedEventArgs e)
{
IntPtr handle = (IntPtr)0x33162; //取得したいWindowのハンドル
//ウィンドウサイズ取得
RECT rect;
bool flag = GetWindowRect(handle, out rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
//ウィンドウをキャプチャする
Bitmap img = new Bitmap(width, height);
Graphics memg = Graphics.FromImage(img);
IntPtr dc = memg.GetHdc();
PrintWindow(handle, dc, 0);
memg.ReleaseHdc(dc);
memg.Dispose();
img.Save("画像.bmp");
}
