Screen Image Capture

Description: Captures the virtual screen (for multi-monitor setups) and returns it as an image which you could then save or draw on top of. Requires the System.Drawing namespace.
Tested Platform: .NET 4.0, Visual Studio 2010, Windows 7
Language: C#
// Returns an image object that is a screenshot of the current virtual screen.

public static Image CaptureScreenImage()
{
    int screenWidth = SystemInformation.VirtualScreen.Width;
    int screenHeight = SystemInformation.VirtualScreen.Height;
    int screenLeft = SystemInformation.VirtualScreen.Left;
    int screenTop = SystemInformation.VirtualScreen.Top;

    Bitmap bitmap = new Bitmap(screenWidth, screenHeight);
          
    Graphics g = Graphics.FromImage(bitmap);
    g.CopyFromScreen(screenLeft, screenTop, 0, 0, new Size(screenWidth, screenHeight));

    return (Image)bitmap; 
}

Submitted: January 16, 2013

Return to the snippets listing