Capturing Screen as Bitmap Image in C#

The easiest way for taking screen snapshot in C# is given in the following code.

            Rectangle workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
            Bitmap screenShot = new Bitmap(workingArea.Width, workingArea.Height);

            Graphics gr = Graphics.FromImage(screenShot);

            gr.CopyFromScreen(0, 0, 0, 0, workingArea.Size);

            return screenShot;

In the above code, first a rectangle is created which equal to the size of the primary screen.
Then a bitmap is created which will store the pixels for the snapshot image.
The Graphics object is created which will store and perform operations on the bitmap object we just created.
The CopyFromScreen function captures the screen, copying all the color pixels to the bitmap of the Graphics object. The parameters to the function are about from where the copying will start in the source and to where it will go in the target. The first two are x and y are starting coordinates for the source and the third and fourth are about the starting coordinates for the target location. The last parameter is about the size of the rectangular region which will be copied, which consist of width and height.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: