I'm trying to make a window screenshot from a non-active desktop (created with CreateDesktop). I use the following code:
Code:
public partial class Form1 : Form
{
[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
public Form1()
{
InitializeComponent();
timer1.Interval = 3000;
timer1.Tick += new System.EventHandler(TimerProc);
timer1.Start();
}
private void TimerProc(object sender, EventArgs e)
{
timer1.Stop();
UsePrintWindow();
}
private void UsePrintWindow()
{
Graphics g = CreateGraphics();
Bitmap bmp = new Bitmap(Size.Width, Size.Height, g);
Graphics memoryGraphics = Graphics.FromImage(bmp);
IntPtr dc = memoryGraphics.GetHdc();
bool success = PrintWindow(Handle, dc, 0);
memoryGraphics.ReleaseHdc(dc);
bmp.Save("c:\\Test\\UsePrintWindow.bmp");
}
}
If I executed this code in non-active desktop on Win7 I receive the following picture:
If I executed this code in non-active desktop on Win2003 I receive the following picture:
Can anybody please advise how to fix it?
P.S. I created MFC-application with the same functionality.
It works fine on Win7 and Win2003.