How to capture the current screen using Windows Forms
Posted by: Suprotim Agarwal ,
on 2/26/2008,
in
Category WinForms & WinRT
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn