Screen Capture Application using C#
Posted on September 11th, 2009 in C#, Tutorials | 9 Comments »
This is a detailed tutorial about making a simple Screen Capture (Screenshot Capture) application in C#. The features are: Take screenshot, view it and save it. We will be making a simple application but if you are little bit good with C# then you can enhance it and make it much better.
Setting up the Form
For this tutorial we will need 2 buttons, a timer, SaveFileDialog and a PictureBox. Here’s the info:
- Add a button first and set its “Text” property to “Take Screenshot” and “Name” property to “button1″.
- Add another button and set “Text” property to “Save Screenshot”, set its “Enabled” property to “False” and “Name” property to “button2″.
- Add a timer and set it “Interval” to “500″ and make sure its “Enabled” property is set to “False”..
- Now add SaveFileDialog and set “Filter” to ” JPEG Files|*.jpg|GIF Files|*.gif|PNG Files|*.png “.
- Finally, add a Picturebox.
That’s it. The form is now set-up. Lets get on with the coding.
The Coding
First of all, declare these variables at the top of the form; i.e: just above “public form1() { …… “.
Graphics g;
// This will get the Size (width & height) of the screen.
Size screenBounds = Screen.PrimaryScreen.Bounds.Size;
public Form1()
{
InitializeComponent();
}
Okay. Now double-click “Take Screenshot” button which will automatically add the “Click” event for it. Use this code:
private void button1_Click(object sender, EventArgs e)
{
// We need to hide the form so that we can
// capture the area behind it.
this.Hide();
timer1.Start();
}
We started the Timer in the above code. Now lets put it to some use.
Double-click the timer which will automatically add the “Tick” event for it. Use this code:
private void timer1_Tick(object sender, EventArgs e)
{
// The screenshot will be stored in this bitmap.
Bitmap capture = new Bitmap(screenBounds.Width, screenBounds.Height);
// The code below takes the screenshot and
// saves it in "capture" bitmap.
g = Graphics.FromImage(capture);
g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds);
// This code assigns the screenshot
// to the Picturebox so that we can view it
pictureBox1.Image = capture;
// The code below make the form visible again, enables the "Save" button and stops the timer.
this.Show();
button2.Enabled = true;
timer1.Stop();
}
The Screenshot capturing and showing it on the picturebox part is done now. Now let’s add the code for the “Save Screenshot” button.
Double-click the Save Screenshot button which adds the “Click” event. Use this code:
private void button2_Click(object sender, EventArgs e)
{
// Show the SaveFileDialog
DialogResult res = saveFileDialog1.ShowDialog();
// Saves the screenshot if the OK button is clicked.
if (res == DialogResult.OK)
{
// This will get the extension of the saved file
string ext = System.IO.Path.GetExtension(saveFileDialog1.FileName);
if(ext == ".jpg")
pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
else if(ext == ".gif")
pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif);
else if(ext == ".png")
pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
That’s it! We are done.
Source Code
Download Now (36 Kb)
Feedback and comments are warmly welcome
.

9 Responses
Whats with the timer click event?
Nice question. I forgot to explain that in my post.
I’ve used the timer to hide the current form and then take screenshot so that the form is not included in the screenshot. Without the timer its not possible to hide the form, take screenshot (behind the current form) and then show the form again. To better understand what I mean, remove the timer code and copy all the code from “timer_tick” event to the “button_click” event and see what happens.
Hope that helps.
Thanks
Thanks for the information, bookmarked your page for updates
Hi,
I have a question, that is not completely related to you post (i like it btw
);
I am looking to capture the screen with an app, but the app must not hide before hand. is there a way to possibly get the form to repaint and get a hold of that image somehow? In affect what I am trying to do is show what is underneath my own form without using transparency.
Thanks
Dom
Hello Dominic,
Sorry, I don’t think if that’s possible. Google it and see if you can find a solution. Do let me know if you find something.
Thanks
I understand that you unnecessarily assign the screen width and height to variables to make it clear in the tutorial how that worked. But you lack consistency as you didn’t assign the screen.bounds.size to a variable. When i started out a couple of years ago that would have thrown me for a loop.
But for simplicity’s sake, the less variables you assign the better. Nice tutorial though, I always like tutorials with a screen shot. Don’t now how I ended up here though. Just got to clicking on links.
I’m always looking for more information about this, thanks for the awesome post, keep them coming!
Yes, you are right. “The less variables you assign the better”
I’ll change that now.
Thanks
Great article! I bet a lot of work went into this post.