Is there a way to take a screenshot of the user's Windows desktop?

Damovisa picture Damovisa · May 19, 2009 · Viewed 12.7k times · Source

I want to provide the user with a scaled-down screenshot of their desktop in my application.

Is there a way to take a screenshot of the current user's Windows desktop?

I'm writing in C#, but if there's a better solution in another language, I'm open to it.

To clarify, I need a screenshot of the Windows Desktop - that's the wallpaper and icons only; no applications or anything that's got focus.

Answer

lc. picture lc. · May 19, 2009

You're looking for Graphics.CopyFromScreen. Create a new Bitmap of the right size and pass the Bitmap's Graphics object screen coordinates of the region to copy.

There's also an article describing how to programmatically take snapshots.

Response to edit: I misunderstood what you meant by "desktop". If you want to take a picture of the desktop, you'll have to:

  1. Minimize all windows using the Win32API (send a MIN_ALL message) first
  2. Take the snapshot
  3. Then undo the minimize all (send a MIN_ALL_UNDO message).

A better way to do this would be not to disturb the other windows, but to copy the image directly from the desktop window. GetDesktopWindow in User32 will return a handle to the desktop. Once you have the window handle, get it's device context and copy the image to a new Bitmap.

There's an excellent example on CodeProject of how to copy the image from it. Look for the sample code about getting and creating the device context in the "Capturing the window content" section.