This tutorial will show you a very simple method of capturing screenshots using C# and .NET 2.0 and saving them as files with different formats, such as PNG, all thanks to the CopyFromScreen() method.
On the web, there are a few tutorials that will show you how to make a screen capture similar to the one that’s created and stored in the clipboard when the “Print Screen” button is pressed. However, you will find that these tutorials are a bit too complicated for such a simple task. In this tutorial we’re going to accomplish the same thing those tutorials do, but with only a few lines of code, thanks to the .NET 2.0 Framework, that introduced the new CopyFromScreen() method.
Start by creating a new Windows Application project in Visual Studio 2005. Add to it a button btnCapture and a SaveFileDialog control entitled saveScreenshot, which will be used to prompt the user to choose a path where he wants the screenshot saved.
Now switch to code view and add the following using statement below the already existent statements:
using System.Drawing.Imaging;
Also, inside the class declare these two objects we’re going to use to capture and store the screenshot:
private static Bitmap bmpScreenshot;
private static Graphics gfxScreenshot;
The button – btnCapture – is where all the action will take place. So inside Visual Studio’s the form designer, double click the button and the Click event will be created. Inside it, use the following code:
// If the user has chosen a path where to save the screenshot
if (saveScreenshot.ShowDialog() == DialogResult.OK)
{
// Hide the form so that it does not appear in the screenshot
this.Hide();
// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save(saveScreenshot.FileName, ImageFormat.Png);
// Show the form again
this.Show();
}
As you can see in the code above, the first thing we do is to show the save dialog to the user so that he can choose the path where he wants the screenshot to be saved. The next thing we do is hide the form so that it does not appear in the screenshot. After that, we are defining the two objects that were declared earlier, with information about the size of the image / screenshot (which is the desktop size). The screenshot is practically taken when the CopyFromScreen() method is called with the appropriate parameters. Then the Bitmap object saves it to the specified path, and format. We choosed PNG since it’s a great file format, however you can choose from the most popular formats (Bmp, Gif, Jpeg, Tiff, etc.).
That’s all you need to capture and save a screenshot in .NET 2.0. In case you prefer to have a look over the entire code of Form1.cs, you can view it below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace ScreenshotCapturer
{
public partial class Form1 : Form
{
private static Bitmap bmpScreenshot;
private static Graphics gfxScreenshot;
public Form1()
{
InitializeComponent();
}
private void btnCapture_Click(object sender, EventArgs e)
{
// If the user has choosed a path where to save the screenshot
if (saveScreenshot.ShowDialog() == DialogResult.OK)
{
// Hide the form so that it does not appear in the screenshot
this.Hide();
// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save(saveScreenshot.FileName, ImageFormat.Png);
// Show the form again
this.Show();
}
}
}
}