Recently I am using an Win API called PrintWindow
, which can screenshot a specific window. In some windows that enabled hardware acceleration, the image will be blank. However, if I changed the third parameter from 0
to 3
, it can work again.
I also posted a demo code written in 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;
}
In the official document, Microsoft only listed PW_CLIENTONLY
, and there is no much information about this parameter, so I am wondering what it is and what values can I use (and their effect).