Question

We need a screenshot of our app for a unit test. CaptureScreen() and CopyFromScreen() somehow ignore the app and return pictures of an empty desktop. So we wrote this to fake a PrtScn keystroke:

public static Bitmap GetAltScreenshot()
{
    Clipboard.Clear();
    SendKeys.SendWait("{PRTSC}");
    while (!Clipboard.ContainsImage())
    {
        Thread.Sleep(500);
    }
    return new Bitmap(Clipboard.GetImage());
}

Alt isn't part of the keystroke, so this should return a bitmap of the entire screen. Yet somehow this snippet returns just the focused window. Which is fine, that solves our problem - but we don't understand how.

Why does this return a shot of just the focused window, instead of the entire monitor?

Was it helpful?

Solution

There is in fact a "reason", turn to the MSDN Library article that documents the key abbreviations you can use. Note the entry for PRINT SCREEN:

{PRTSC} (reserved for future use)

The is a somewhat clumsy way of saying "We know it doesn't work, maybe will fix that some day". That day hasn't yet arrived. So you are probably testing the failure mode of this key and actually like the way it works. This is of course not healthy, they may actually fix the problem some day and break your program.

Do note the Note about the <appSettings> entry that you can add to your .config file, further down that same MSDN page. I suspect, but do not know for a fact, that the SendInput method is more reliable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top