How to capture the current screen using Windows Forms

Posted by: Suprotim Agarwal , on 2/26/2008, in Category WinForms & WinRT
Views: 24007
Abstract: 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.
How to capture the current screen using Windows Forms
 
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.
C#
        private void btnCapture_Click(object sender, EventArgs e)
        {
            Graphics graph = null;
            try
            {
                Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                graph = Graphics.FromImage(bmp);
                graph.CopyFromScreen(0,0, 0, 0, bmp.Size);
                SaveImage(bmp);
            }
            finally
            {
                graph.Dispose();
            }
 
 
        }
 
        private void SaveImage(Bitmap b)
        {
            b.Save(@"C:\1.bmp");
        }
VB.NET
            Private Sub btnCapture_Click(ByVal sender As Object, ByVal e As EventArgs)
                  Dim graph As Graphics = Nothing
                  Try
                        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)
                        SaveImage(bmp)
                  Finally
                        graph.Dispose()
                  End Try
 
 
            End Sub
 
            Private Sub SaveImage(ByVal b As Bitmap)
                  b.Save("C:\1.bmp")
          End Sub
 
That was quiet easy right. You have built the ‘Print Screen’ functionality successfully using Windows Forms. I hope this article was useful and I thank you for viewing it.
If you liked the article, please subscribe to my RSS feed over here.
 
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Raghunath on Tuesday, March 25, 2008 5:02 AM
I didnot get the result what you said. Improvement needed.
Thanks and Regards,
Raghu
Comment posted by Darshan Shah on Tuesday, June 17, 2008 4:44 AM
Hi this was a nice article. i would like to know how can we capture the screen for the active window and not the entire screen at a time. Also is it possible to capture the entire screen of a current windows form even there is scrolling required to view the entire content of the form.
Comment posted by Suprotim Agarwal on Tuesday, June 17, 2008 11:51 AM
Hi Darshan..Get some ideas from over here:
http://www.vbdotnetheaven.com/Uploadfile/mgold/ScreenCaptureUtilityVB11162005063518AM/ScreenCaptureUtilityVB.aspx
http://blogs.geekdojo.net/brian/articles/Cropper.aspx
Comment posted by how to capture active window using asp.net on Friday, June 27, 2008 7:34 AM
how to capture active window(aspx page) using asp.net
Comment posted by how to capture active window(aspx page ) using asp.net on Friday, June 27, 2008 7:36 AM
how to capture active window(aspx page ) using asp.net
Comment posted by Bobby on Friday, February 27, 2009 3:04 PM
good coding. Thanks
But can you tell me how to resize the captured image..?
Comment posted by Wendy on Sunday, April 19, 2009 3:40 PM
Nice. But suppose you want to take a screen grab of the currently active application window? You can copy this window to the system clipboard by pressing Alt + PrtScn. Is there a .NET equivalent to doing this?
Comment posted by paranthagan on Tuesday, December 29, 2009 4:39 AM
Really very useful stuff.Thanks for providing this.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel