C#printWindow黒い画像を返します
-
26-10-2019 - |
質問
私はWindows XPを使用しており、ウィンドウを格付けしようとしています。
しかし、ウィンドウをキャプチャすると、ウィンドウのタイトル(名前とアイコン)が表示されますが、ウィンドウのコンテンツ全体が黒です。
画像を保存しようとすると、画像全体が透明です。
これは私のコードです:
[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
public void CaptureWindow(IntPtr handle)
{
Rectangle rect = new Rectangle();
GetWindowRect(handle, ref rect);
rect.Width = rect.Width - rect.X;
rect.Height = rect.Height - rect.Y;
try
{
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
Graphics memoryGraphics = Graphics.FromImage(bmp);
IntPtr hdc = memoryGraphics.GetHdc();
PrintWindow(handle, hdc, 0);
ResultsPB.Image = bmp;
memoryGraphics.ReleaseHdc(hdc);
}
catch (Exception)
{
throw;
}
}
解決
見る C#ダイレクト3D画面のキャプチャ。印刷画面は、ハンドルではなく、可視領域をキャプチャします。
DirectXとOpenGLは、ハードウェアを介して直接描画します。 PrintScreenを使用すると、Windowsで管理されているハンドル画面のみをキャプチャできます。
目に見える領域のみが必要な場合は、graphics.capturefromscreenを使用します。
bitbltとprintscreenを試してみました。 http://magnum.dimajix.de/download/demos.shtml 成功せずに。 PrintScreenは空白のビットマップを返し、BitBltは古いキャプチャを返します(おそらく最初と唯一のWM_Paintメッセージから)。
ゲームを開始して、窓のメッセージを聞いてみてください。 wm_paintメッセージがないことがわかります。そのため、Windowsは何かが変更されているかどうかさえ知りません。
所属していません StackOverflow