A simple screen capture application c#

Will be posting article on how to create simple application, with full working code in C#
And welcome to anyone who want to post their own article
Post Reply
User avatar

Topic author
Superl
Site Admin
Site Admin
Man of action
Man of action
Posts: 1331
Joined: Sat Apr 16, 2011 7:49 am
12
Location: Montreal, Canada
Contact:

A simple screen capture application c#

#1457

Post by Superl »

Image
Introduction
Not much to say but to create this tool to capture any part and size of the screen you want, is very simple and can be accomplish in a few minutes.

Creating the application
1 - Create a new windows application project in Visual Studion, for this example I called it ScreeCapture, add a button and an OpenFileDialogue to the form.

2 - Add these using to the namespace

Code: Select all

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;
3 - Add variable to store output file name

Code: Select all

private string strFilename;
4 - Add Property for storing and retrieving output file name

Code: Select all

public string filename
        {
            get { return strFilename; }
            set { strFilename = value; }
        }
5 - Now that we declare all the needed variables go to the form design and double click the button and this will create Click event of the button1 control.
Now lets define the proprety for the OpenFileDialogue
Add this in the button event

Code: Select all

saveFileDialog1.Filter = 
 "JPG File (*.jpg)|*.jpg|BMP File (*.bmp)|*.bmp|PNG File (*.png)|*.png|GIF File(*.gif)|*.gif|Tiff File (*.tif)|*.tif";

saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
Now lets add a check if user clicked OK button and Get the file name and store it in are variable

Code: Select all

if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                // Get the filename and store it
                filename = saveFileDialog1.FileName;
            }
            else // If Cancel was selectedreturn
                return;
6 - Now that we have the file name the user want to save the capture lets create the graphic to hold the capture.
Add

Code: Select all

// Creating new bitmap with the size of the form                    
Bitmap bmp = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);

// Creating graphic object from the image
Graphics gr = Graphics.FromImage(bmp);
7 - Now lets take the snapshot

Code: Select all

gr.CopyFromScreen(this.Left, this.Top, 0, 0, this.Size, CopyPixelOperation.SourceCopy);
8 - Now the only thing left to do is to save the capture under the given file name

Code: Select all

switch (saveFileDialog1.FilterIndex)
                    {
                        case 1:
                            bmp.Save(filename, ImageFormat.Jpeg);
                            break;
                        case 2:
                            bmp.Save(filename, ImageFormat.Png);
                            break;
                        case 3:
                            bmp.Save(filename, ImageFormat.Bmp);
                            break;
                        case 4:
                            bmp.Save(filename, ImageFormat.Gif);
                            break;
                        case 5:
                            bmp.Save(filename, ImageFormat.Tiff);
                            break;

                    }
You can use this example to create a more advance screen capture tool.

Hope you enjoy this article and you can download the full code in Visual Studio 2012 solution
ScreenCapture.rar
ScreenCapture Code
(46.06 KiB) Downloaded 1465 times
ScreenCapture.rar
ScreenCapture Code
(46.06 KiB) Downloaded 1465 times
[/align]


Come and say hello in here
Any donation will help click here please.

Have a nice day :103:
User avatar

TERMINATOR T-101
Helper
Helper
Posts: 43
Joined: Sun Mar 25, 2012 12:11 pm
11

Re: A simple screen capture application c#

#1551

Post by TERMINATOR T-101 »

Thanks from today onwards i willl go thru your tutorials :-bd
User avatar

Topic author
Superl
Site Admin
Site Admin
Man of action
Man of action
Posts: 1331
Joined: Sat Apr 16, 2011 7:49 am
12
Location: Montreal, Canada
Contact:

Re: A simple screen capture application c#

#1555

Post by Superl »

Hope I will have time to make more
But during next winter I will make more


Come and say hello in here
Any donation will help click here please.

Have a nice day :103:
User avatar

TERMINATOR T-101
Helper
Helper
Posts: 43
Joined: Sun Mar 25, 2012 12:11 pm
11

Re: A simple screen capture application c#

#1565

Post by TERMINATOR T-101 »

No prob. If you make pdf tutorial it would be of great help for me. :)
Post Reply

Return to “Coding Forum”