Windows APIを呼び出して、C#.NETを使用してウィンドウのスクリーンショットを撮ろうとしています。私は次のコードを思いついた:
public void ScreenshotWindow(IntPtr windowHandle) {
Rect Rect = new Rect();
GetWindowRect(windowHandle, out Rect);
int width = Rect.right - Rect.left;
int height = Rect.bottom - Rect.top;
IntPtr windowDeviceContext = GetWindowDC(windowHandle);
IntPtr destDeviceContext = CreateCompatibleDC(windowDeviceContext);
IntPtr bitmapHandle = CreateCompatibleBitmap(windowDeviceContext, width, height);
IntPtr oldObject = SelectObject(destDeviceContext, bitmapHandle);
BitBlt(destDeviceContext, 0, 0, width, height, windowDeviceContext, 0, 0, CAPTUREBLT | SRCCOPY);
SelectObject(destDeviceContext, oldObject);
DeleteDC(destDeviceContext);
ReleaseDC(windowHandle, destDeviceContext);
Image screenshot = Image.FromHbitmap(bitmapHandle);
DeleteObject(bitmapHandle);
screenshot.Save("C:\\Screenshots\\" + windowHandle.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
これは、ウィンドウのスクリーンショットを取得するための一般的な一連のWindowsAPI呼び出しです。
スクリーンショットを取得する別の方法を探しているわけではないことに注意してください。Graphics.CopyFromScreen()
この(固定)アプローチの速度と.NETメソッドの速度を比較したいと思います。
問題は、Windows 7を実行している最大化されたウィンドウのスクリーンショットを撮ろうとすると、タイトルバーと境界線(および場合によってはウィンドウの他の部分)が黒くなることです。
これは、ウィンドウが階層化されているか、ウィンドウのタイトルバーがウィンドウ自体によって管理されているため、ピクセル情報にアクセスできないことが原因だと思います(どこかで読んだように)。
誰かがこの振る舞いを修正する方法を知っていますか?