I am currently taking a screenshot of an external application with PrintWindow and i want to save it to my desktop.
This is my code :
// Get proc
Process proc = Process.GetProcessesByName("procName").Single();
IntPtr hWnd = proc.MainWindowHandle;
// Restore proc if minimised
int style = GetWindowLong(hWnd, GWL_STYLE);
if ((style & WS_MINIMIZE) == WS_MINIMIZE)
ShowWindow(hWnd, WindowShowStyle.Restore);
// Get RECT
RECT rect;
GetWindowRect(new HandleRef(this, hWnd), out rect);
// Get screenshot
int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
{
IntPtr dc = g.GetHdc();
if (!PrintWindow(hWnd, dc, 0))
{
int error = Marshal.GetLastWin32Error();
var exception = new System.ComponentModel.Win32Exception(error);
Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
return;
}
//Thread.Sleep(200);
bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.jpeg", ImageFormat.Jpeg);
panel1.BackgroundImage = bmp;
g.ReleaseHdc(dc);
}
The panel1 shows me the good image, the actual screenshot of the application. When i go to my desktop, i find a test.jpeg but it's all black. Why?
Thanks !