Geeks With Blogs
Geekette Mai Blog
The Graphics class exposes a method called CopyFromScreen(). According to MSDN, this method performs a bit-block transfer of the color data, corresponding to a rectangle of pixels, from the screen to the drawing surface of the Graphics. In short, it is used to capture a rectangle and draw it to an image. Let us see how to use this method to capture the current screen.
 The signature of this method is as follows :
 public void CopyFromScreen(
    int sourceX,
    int sourceY,
    int destinationX,
    int destinationY,
    Size blockRegionSize)
 where sourceX and sourceY are the x and y coordinate of the point at the upper-left corner of the ‘source’ rectangle. The destinationX and destinationY are the x and y coordinate of the point at the upper-left corner of the ‘destination’ rectangle.
 In this example, we will be creating a bitmap equal to the height and width of the primary display. We then create a new Graphics from the specified bitmap. The Graphics.CopyFromScreen() is used to capture the screen and draw it in the bitmap. Once done, the image is saved to the desktop. Let us see some code.
 Create a form and drop a button on it from the toolbar. Rename the button as btnCapture.
        private void btnCapture_Click(object sender, EventArgs e)
            Graphics graph = null;
                Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                graph = Graphics.FromImage(bmp);
                graph.CopyFromScreen(0,0, 0, 0, bmp.Size);
        private void SaveImage(Bitmap b)
            Private Sub btnCapture_Click(ByVal sender As Object, ByVal e As EventArgs)
                  Dim graph As Graphics = Nothing
                        Dim bmp As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
                        graph = Graphics.FromImage(bmp)
                        graph.CopyFromScreen(0,0, 0, 0, bmp.Size)
                  End Try
            End Sub
            Private Sub SaveImage(ByVal b As Bitmap)
          End Sub
I take lot of screenshots and its time consuming to press the screenshot button, paste it in MSPaint or any other Image editing software and then save it. The pain is even more when I need to use two Buttons for a screenshot (Fn + PrintScreen) in my Laptop ;-). So I thought I will develop my own screenshot capturing tool which will allow me to take screenshots as well as save it in just two clicks. In this article I am going to show you how easy it is to develop your own tool using .NET. Let’s get started.
Step 1: Fire up Visual Studio 2008 and Create a new Windows Application.
Step 2: From the toolbox, add a ‘ContextMenuStrip’ and a ‘NotifyIcon’ to the form.
Step 3: Choose an icon for the’ NotifyIcon’ using the Icon property. Change the ‘ContextMenuStrip’ property of the NotifyIcon to ‘contextMenuStrip1’ (which we added in Step 2)
Step 4: Add a new menu item to ContextMenuStrip1 control named “Grab Screenshot” as shown in the screen below:
Now double click this MenuItem and add the following code to its click event:
private void grabScreenShotToolStripMenuItem_Click(object sender, EventArgs e)
    Bitmap bmpSS = null;
    Graphics gfxSS = null;
        bmpSS = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
        gfxSS = Graphics.FromImage(bmpSS);
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.Filter = "JPeg Image|*.jpg";
        saveDialog.Title = "Save Image as";
        if (saveDialog.FileName != string.Empty)
            bmpSS.Save(saveDialog.FileName, ImageFormat.Jpeg);
    catch (Exception ex)
Private Sub GrabScreenshotToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GrabScreenshotToolStripMenuItem.Click
        Dim bmpSS As Bitmap
        Dim gfxSS As Graphics
        bmpSS = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb)
        gfxSS = Graphics.FromImage(bmpSS)
        gfxSS.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
        Dim saveDialog As New SaveFileDialog
        saveDialog.Filter = "JPeg Image|*.jpg"
        saveDialog.Title = "Save Image as"
        If saveDialog.FileName <> "" Then
            bmpSS.Save(saveDialog.FileName, ImageFormat.Jpeg)
        End If
    End Sub
Note: Make sure you import System.Drawing and System.Drawing.Imaging namespaces.
In the code above, we create a new Bitmap which is equal to the width and height of the primary screen and then we create a new graphics using the Bitmap. We then use the CopyFromScreen() to capture the screen. Once this step is done, we create an object of SaveFileDialog and display it to the user to choose the location and save the file as an .jpeg image.
Step 5: Add the following code to the Form load event which will hide the form and remove it from taskbar.
private void Form1_Load(object sender, EventArgs e)
    this.ShowInTaskbar = false;
    this.WindowState = FormWindowState.Minimized;
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ShowInTaskbar = False
        Me.WindowState = FormWindowState.Minimized
    End Sub
Now run the program and right click the Icon that appears in your status bar. Click on the ‘Grab ScreenShot’ and save the screenshot to the desired location.
Status Screenshot
The next time you want a screenshot, remember it’s just two clicks away!
Posted on Thursday, January 14, 2010 9:36 PM | Back to top

Comments on this post: screenshot using Windows Forms

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Mai Nguyen | Powered by: