Capture screen using C#

This tiny example shows how easy is to make a desktop screenshot and save it to a file using the .NET framework.

What we will be doing is creating an instance of the Graphics class, call the CopyFromScreen function to acquire an screenshot of the desktop (just like pressing the [Print Screen] key) and, for saving it to a file, we will use the Save function of an instance of the Bitmap class, associated to the Graphics instance we created first. It’s easy, let’s have a look at the code :).

// Bitmap and Graphics instances
Bitmap bmpScreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics gfxScreen = Graphics.FromImage(bmpCaptura);

// Copy entire screen
gfxScreen.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);

// Save it to a file

// Show it in a picture box
pbScreen.Image = bmpScreen;

See how simple? I told you ;).

Be the first to comment

Leave a Reply

Your email address will not be published.