How to take a screenshot in C#

The subsequent code snippet can be used to take screenshots in C#.Net. The screenshots, you capture with the following snippet, equal to the screenshots you take by pressing the print-key on your keyboard. (Certainly aside from the fact that the screenshot won’t be in your clipboard, but you will get a bitmap object, which can be used to manipulte in anyway you like in C#.)

Because the the source code is very short, I’ll let it speak for itself.

private Bitmap Screenshot()
{
    //Create a bitmap with the same dimensions like the screen
    Bitmap screen = new Bitmap(SystemInformation.VirtualScreen.Width,
                                         SystemInformation.VirtualScreen.Height);

    //Create graphics object from bitmap
    Graphics g = Graphics.FromImage(screen);

    //Paint the screen on the bitmap
    g.CopyFromScreen(SystemInformation.VirtualScreen.X,
                             SystemInformation.VirtualScreen.Y,
                             0, 0, screen.Size);
    g.Dispose();

    //return bitmap object / screenshot
    return screen;
}

If you have questions concerning the source or maybe know an easier way to solve the task, let me know it.

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked