6

I am trying to get a screenshot of the whole virtual screen. This means, an image of not just the primary screen, but every screen connected to the computer.

Is there a way to do that? I tried using this, but it didn't work:

Bitmap b = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
Graphics g = Graphics.FromImage(b);
this.Size = new Size(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
g.CopyFromScreen(0, 0, 0, 0, b.Size);
3
  • What does exactly not work? Are you getting some kind of exception?
    – Marcel
    Commented Mar 31, 2011 at 12:58
  • it only captures primary screen...
    – John
    Commented Mar 31, 2011 at 13:10
  • didnt know about "virtualscreen", thanks dude
    – Nacht
    Commented Nov 19, 2014 at 11:37

3 Answers 3

6

The documentation says: Graphics.CopyFromScreen(Int32, Int32, Int32, Int32, Size): Performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics." But the virtual screen is not necessarily a rectangle: imagine two monitors with 1920x1200 and 1280x1024 resolutions. So what you need to do is create a bitmap like you do, then enumerate your monitors and execute CopyFromScreen() for each of them.

Edit: If, for instance, you have two monitors, the one having 1280x1024 resolution standing on the left of 1920x1200 one, then the coordinates of the former would be (-1280,0) - (0, 1024). Therefore you need to execute memoryGraphics.CopyFromScreen(-1280, 0, 0, 0, s); where s is the Size(1280,1024). For the second one you need to call memoryGraphics.CopyFromScreen(0, 0, *1280*, 0, s); and s would be the Size(1920, 1200). Hope this helps.

2

Like Igor and Hans have said, you have to indicate the source coordinate :

Bitmap screenshot = new Bitmap(
    SystemInformation.VirtualScreen.Width, 
    SystemInformation.VirtualScreen.Height, 
    PixelFormat.Format32bppArgb);

Graphics screenGraph = Graphics.FromImage(screenshot);

screenGraph.CopyFromScreen(
    SystemInformation.VirtualScreen.X, 
    SystemInformation.VirtualScreen.Y, 
    0, 
    0, 
    SystemInformation.VirtualScreen.Size, 
    CopyPixelOperation.SourceCopy);
2

Igor is right, passing 0, 0 for the SourceX/Y arguments isn't correct. Iterate the Screen instances in the Screen.AllScreens property to find the bounding rectangle. Beware that CopyFromScreen() has a bug, it cannot capture layered windows (the kind that has TransparencyKey or Opacity set). Check my answer in this thread for a workaround.

Beware that capturing the entire desktop isn't always practical, you'll get lots of black when the screens are not arranged in a perfect rectangle and an OutOfMemory exception is not uncommon on a 32-bit machine with high resolution displays.

2
  • If the lots of black works for <kbd>Print Scrn</kbd>, i assume it should be good enough for OP.
    – Ian Boyd
    Commented Mar 31, 2011 at 15:34
  • Hmm, the black of course only shows up on his customer's machine. Commented Mar 31, 2011 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.