8

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?

4444
  • 3,523
  • 10
  • 27
  • 43
  • You should look at: http://stackoverflow.com/questions/5049122/how-to-capture-the-screen-shot-using-net – George Johnston Jul 19 '13 at 18:52
  • @GeorgeJohnston Believe me we've tried using `g.CopyFromScreen` before, as well as every other capture method we could find. They all performed fine on my machine, but gave weird results on other machines within the company. – 4444 Jul 19 '13 at 18:56
  • Could it possibly be that the SendKeys class just interprets the "{PRTSC}" key string to mean the Alt + PrtScn key combination? Also, you might be able to just manually set the focus to be the whole screeen? [link](http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait.aspx) at the first note. – Ian Panzica Jul 22 '13 at 15:40
  • Have you tried doing a `SendKeys.Flush()` before sending the {PRTSC}, it's kind of a long shot but just to be sure you don't have other messages in the queue it might at least be worth ruling out. – Brian Maupin Jul 22 '13 at 15:59
  • @Brian `SendKeys.Flush()` has no effect. The code works perfectly with or without it. – 4444 Jul 22 '13 at 16:07

1 Answers1

8

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.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • 1
    Yes, I am aware of that "reason" (In fact, I think an old post from you pointed me there) but I was more or less curious to know the inner workings of this mystery. I suppose this is as much as Microsoft is willing to let us know at this. – 4444 Jul 22 '13 at 16:34
  • I really don't know the underlying reason. The journaling hook they use without the appsetting is, erm, quirky. – Hans Passant Jul 22 '13 at 16:39