Capture a Region of Screen Using C#

  • Aman
  • Updated date Mar 24, 2014

Introduction

In our previous article I showed how to implement a Windows Forms application to capture a screen using C#.

We are now extending that concept a step further. In this article we will implement a Windows application to capture a region of the screen, not the full screen.

It is a very short demo, especially for beginners.

Procedure

Step 1: Create a new “Windows Forms application” in Visual Studio and name it as you choose (I named it ScreenRegionDemo). Now a new form is generated.

Step 2: Now go to the toolbox and add two NumericUpDown Controls for specifying the region up to which we want to capture the screen area, two label controls and label them Height and Width respectively and a Button Control for the project also to resize the window size.

The form will look like this:

Form

Step 3: Add the following using directive:
 

using System.Drawing.Imaging;

using System.Drawing;

using System;

Step 4: Navigate to the code file (Form1.cs) and two integer type variables that will keep the value from the two NumericUpdown Controls that we have added to our project.

public partial class Form1 : Form

{

    int xheight, xwidth;         

    public Form1()

    {

        InitializeComponent();           

    }

}

Step 5: Now navigate to the Button Event Handler and add the following code:

private void buttonCapture_Click(object sender, EventArgs e)

{                                           

     //Storing the Value from both the NumaricUpDown Control

      xheight = (int)numericUpDownHeight.Value;

      xwidth = (int)numericUpDownWidth.Value;

     //Creating a Method name CaptureMyScreen

     CaptureMethod(xheight,xwidth);

}

private void CaptureMyScreen(int xheight,int xwidth)

{

}

Step 6: Now add the following code to the method that we created (CaptureMyScreen()):

private void CaptureMyScreen(int xheight,int xwidth)

{

    try

    {

        //Creating a new Bitmap object

        Bitmap captureBitmap = new Bitmap(xheight, xwidth, PixelFormat.Format32bppArgb);

        //Bitmap captureBitmap = new Bitmap(int width, int height, PixelFormat);  

       //Creating a Rectangle object which will  

       //capture our Current Screen

       Rectangle captureRectangle = Screen.AllScreens[0].Bounds;

       //Creating a New Graphics Object

       Graphics captureGraphics = Graphics.FromImage(captureBitmap);

       //Copying Image from The Screen

       captureGraphics.CopyFromScreen(captureRectangle.Left,captureRectangle.Top,0,0,captureRectangle.Size);

       //Saving the Image File (I am here Saving it in My E drive).

       captureBitmap.Save(@"E:\Capture.jpg",ImageFormat.Jpeg); 

       //Displaying the Successfull Result 

       MessageBox.Show("Screen Captured");

   } 

  catch (Exception ex)

  {

        MessageBox.Show(ex.Message);

   }

}

Step 7: Now compile it and run it.

You will get your captured screen.

That's all for this article. I am embedding the source file so that you can go through it.