我在 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;
}
}