PrintWindow()
的 Win32 API,它可以截取特定窗口。在某些启用了硬件加速的窗口中,图像将为空白。但是,如果我将第三个参数从 0
更改为 3
,它就可以再次工作。
我还发布了用C#编写的演示代码:
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static Bitmap GetScreenshot_PrintWindow(IntPtr hWnd, Rectangle screenBounds)
{
RECT windowRect;
if (!GetWindowRect(hWnd, out windowRect))
return null;
Rectangle windowBounds = new Rectangle(windowRect.Left, windowRect.Top, windowRect.Right - windowRect.Left, windowRect.Bottom - windowRect.Top);
Rectangle intersection = Rectangle.Intersect(screenBounds, windowBounds);
if (intersection.IsEmpty)
return null;
Bitmap screenshot = new Bitmap(intersection.Width, intersection.Height);
using (Graphics g = Graphics.FromImage(screenshot))
{
IntPtr hdc = g.GetHdc();
PrintWindow(hWnd, hdc, 3);
g.ReleaseHdc(hdc);
}
return screenshot;
}
在官方文档中,微软只列出了
PW_CLIENTONLY
,并没有太多关于这个参数的信息,所以我想知道它是什么,我可以使用哪些值,以及它们的作用是什么。
我可以在文件头中找到另一个未提及的标志:
#define PW_CLIENTONLY 0x00000001
#if(_WIN32_WINNT >= 0x0603)
#define PW_RENDERFULLCONTENT 0x00000002
#endif /* _WIN32_WINNT >= 0x0603 */
此代码仅表示
PW_RENDERFULLCONTENT
标志只能在 Windows NT 6.1 (Windows 8.1) 及更高版本上使用。因此,如果您将其设置为 2,它将被视为有效。但文档讨论了另一个未提及的标志:
...如果指定了
标志...PW_PRINTCLIENT
我认为有 3 件事可能导致此异常:
PW_PRINTCLIENT
标志的值为3(Windows API标头中没有任何具有该名称的标志)。希望能帮到你。