Screenshot

Obtaining a screenshot is a rather easy process and only takes a few lines of code. Here I will show you how to create a method you can call whenever you want to take a screenshot.

We are going to be using a Graphics object in our code, so you must use the System.Drawing.Imaging namespace in your project, along with the other needed ones:

using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;

First we setup a Bitmap object that has the dimensions of the screen:

Bitmap ScreenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

Then we use that to setup a Graphics object:

Graphics ScreenGraphics = Graphics.FromImage(ScreenBitmap);

The Graphics class contains a CopyFromScreen method, which we will use to get the screens contents:

ScreenGraphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

Now that we have the image, we can dispose of the Graphics object:

ScreenGraphics.Dispose();

So here is a method we could use, in which you can specify the file path and name and then the ImageFormat (such as png, jpg, bmp, etc.):

public void SaveScreenImage(string FileName, ImageFormat Format)
{
Bitmap ScreenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics ScreenGraphics = Graphics.FromImage(ScreenBitmap);
ScreenGraphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
ScreenGraphics.Dispose();
ScreenBitmap.Save(FileName,Format);
}

Another can obtain the image for us and return it for use somewhere else in the application:

public Bitmap GetScreenImage()
{
Bitmap ScreenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
Graphics ScreenGraphics = Graphics.FromImage(ScreenBitmap);
ScreenGraphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
ScreenGraphics.Dispose();
return ScreenBitmap;
}

These methods are easily extendable to include a multiple screen scenario, such as this one:

public Bitmap MultipleScreenShot()
{
Rectangle MultipleScreens = new Rectangle();
 
foreach (Screen AvailableScreen in Screen.AllScreens)
{
MultipleScreens = Rectangle.Union(MultipleScreens, AvailableScreen.Bounds);
}
 
Bitmap ScreenBitmap = new Bitmap(MultipleScreens.Width, MultipleScreens.Height, PixelFormat.Format32bppArgb);
Graphics ScreenGraphics = Graphics.FromImage(ScreenBitmap);
ScreenGraphics.CopyFromScreen(MultipleScreens.X, MultipleScreens.Y, 0, 0, MultipleScreens.Size, CopyPixelOperation.SourceCopy);
ScreenGraphics.Dispose();
return ScreenBitmap;
}

The Rectangle.Union method returns a rectangle that is the result of the merging of two rectangles. So, for each screen we find we add it on to the size of the area to capture.