Easy Screen Capture Class - Capture Screen, Desktop Region, Form, or Controls
Here is a class that contains very easy methods to get screen captures. It is not code originally written by me, it was actually found Here. It was originally written in C#, and there was supposedly a VB.NET example in there, however it just looked as if it was ran through some code converter that didn't work. I cleaned it up in order for it to work in .NET, replaced all the API declarations with the correct working ones, and even added a CaptureDeskTopRectangle function to it for desktop regions.
The file has its own namespace, "ScreenShot", which includes three seperate classes:
ScreenCapture - Main class, contains functions for obtaining the actual images GDI32 = Helper Class with various GDI Functions that are used USER32 - Helper Class with various User32 Functions used
The ScreenCapture has five exposed functions and methods that enables you to get captures:
CaptureWindow - grabs image of object by its handle CaptureScreen - grabs image of entire screen CaptureWindowToFile - captures image of object by its handle straight to file CaptureScreenToFile - captures image of screen straight to file CaptureDesktopRectangle - captures desktop region by a passed in rectangle
***.NET 2005 has a .CopyFromScreen method in the Graphics class that allows you to get a desktop region, however, there is no way in 2003, which is why CaptureDesktopRectangle was needed...
Sample code showing how to use each method is below, the class is available for download at the end of the post.
VB Code:
Dim SC As New ScreenShot.ScreenCapture
'grabs image of object handle (in this case, the form)
Dim MyWindow As Image = SC.CaptureWindow(Me.Handle)