I created the SCapture class to allow us capture still images from the screen. The SCapture class uses some Api methods to implement the capture. The main reason I used the Api functions such as BitBlt to build the bitmap image because the .Net way of capturing an image does not capture transparent image as transparent; check it here.
Class shared methods:
- FullScreen - Captures the full screen (all monitors in a single image).
- DisplayMonitor- Captures a display monitor.
- ActiveWindow – Captures the active window.
- Window– Captures a window specified by the handle or a point (overloaded).
- Control– Captures a control of a window specified by a handle or a point (overloaded).
- ScreenRectangle– Captures a rectangle image from the screen.
- All methods can capture images that include the cursor. New
Using the code:
vb Code:
Private Sub ControlButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ControlButton.Click
Try
'Capture the image of this button including the cursor.
Dim img As Image = SCapture.Control(Control.MousePosition, True)
'Save the captured image.
img.Save(filePath, Drawing.Imaging.ImageFormat.Png)
'Also display the captured image in a PictureBox.
Me.DisplayPictureBox.BackgroundImage = img
Catch ex As Exception
'Show a MessageBox if the capture of image failed.
MessageBox.Show("Failed to capture the control!" _
& Environment.NewLine & ex.Message, "Capture Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Author name:
Arman Ghazanchyan
Update history:
09/17/2007
- I added a method DisplayMonitor that implements a monitor image capture.
- Also the FullScreen method is improved to capture a single image of all monitors that are installed on the system.
06/29/2008
- I added an ability to all methods to capture images that include the cursor.
07/21/2008
- I just changed the class name from ICapture to SCapture.
08/05/2008
- I added an overloadable Window method that takes a point as one of its arguments and returns the bitmap of a window at the point.
- Changed all capture methods return types from Image to Bitmap.
You can check the demo project for the examples of how to capture images with various methods.
Please feel free to rate this post
, comment and notify me about any problem associated with it.
Download the latest version here