I am building a WinForms application that records steps of an external process by taking its screenshots every 500 milliseconds. I am using following code:
Bitmap bmp = new Bitmap(width, height,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.left,
rect.top,
0,
0,
new Size(width, height),
CopyPixelOperation.SourceCopy);
The code is working fine but the only problem is that when I open a dialog box from the external process's window (For ex: Opening Save As...
dialog box in Notepad), the screenshot freezes to the original window instead of showing dialog box.
My theory is that because of following code that I am using to detect if the application lost focus then just revert to last saved screenshot:
if (GetForegroundWindow() != proc.MainWindowHandle) //proc is just a process from system process list by Process.GetProcessesByName()
{
return LastScreenShot;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
But this code is necessary for showing user only the application that is being recorded not any other application that is being dragged inside the recording area or the part of desktop inside the recording area. Also, when I click the menu, it sometimes shows the menu freezed in faded position, sometime not showing at all or showing but navigational highlighting not visible in the screenshot.
So is there any way I can solve this problem?
Similar question is asked here Screen Capture Not Capturing Dialog Boxes in My application but it doesn't solve my problem because answer is using the same code and my application does not take the screenshot of the whole desktop.