Save an image of the computer’s screen in a file in C#

Save an image of the computer's screen in a file

You can save an image of the computer’s screen by using a Graphics object’s CopyFromScreen method. The following GetScreenImage method uses CopyFromScreen to grab an image of the screen.

// Grab the screen's image.
private Bitmap GetScreenImage()
    // Make a bitmap to hold the result.
    Bitmap bm = new Bitmap(

    // Copy the image into the bitmap.
    using (Graphics gr = Graphics.FromImage(bm))
            0, 0,

    // Return the result.
    return bm;

The code uses Screen.PrimaryScreen.Bounds to get the screen’s dimensions (that’s the only tricky part) and makes a bitmap to fit. It creates a Graphics object associated with the bitmap and uses its CopyFromScreen method to copy an image of the screen into the bitmap. The method then returns the bitmap.

(You might need to modify this code if you use multiple monitors. If you do, let me know how it works out.)

The main program is relatively simple. When you click the program’s Get Screen Image button, the following code executes.

private void btnGetScreenImage_Click(object sender, EventArgs e)
    // Hide this form.

    // Let the user pick a file to hold the image.
    if (sfdScreenImage.ShowDialog() == DialogResult.OK)
        // Get the screen's image.
        using (Bitmap bm = GetScreenImage())
            // Save the bitmap in the selected file.
            SaveImage(bm, sfdScreenImage.FileName);

    // Show this form again.

The code hides the program’s form (so it doesn’t appear in the screen capture). It then displays a save file dialog to let you select the output file. If you select a file, the program uses GetScreenImage to get the screen’s image and saves it into the file. (See Save images with an appropriate format depending on the file name’s extension in C# for a description of the SaveImage method.)

Download Example   Follow me on Twitter   RSS feed

This entry was posted in graphics, image processing and tagged , , , , , , , , , , , . Bookmark the permalink.

1 Response to Save an image of the computer’s screen in a file in C#

  1. Pingback: Use a notify icon to capture a screen image in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.