Screen Capture Application

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:

  1. Add a button first and set its “Text” property to “Take Screenshot” and “Name” property to “button1″.
  2. Add another button and set “Text” property to “Save Screenshot”, set its “Enabled” property to “False” and “Name” property to “button2″.
  3. Add a timer and set it “Interval” to “500″ and make sure its “Enabled” property is set to “False”..
  4. Now add SaveFileDialog and set “Filter” to ” JPEG Files|*.jpg|GIF Files|*.gif|PNG Files|*.png “.
  5. 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 :) .

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Reddit
  • Yahoo! Buzz
  • StumbleUpon
  • LinkedIn
  • Twitter