I am on windows XP and I am trying to captrue a window.
But when I capture a window, I get the window title (Name & Icon) but the entire content of the window is black.
When trying to save the image, the whole image is transparent.
This is my code :
[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;
}
}