SCapture Class

The SCapture class provides us with methods to capture still images from the screen, with or without the cursor. The class can capture windows, controls, rectangle area of a screen, the entire desktop that includes all monitors, shortcut menus etc. Most of the methods are overloaded that take a point or a control as their parameters.

Screen Shots


Background:

The class uses some windows API methods to implement the capture. The main reason for using API functions such as BitBlt is to build the bitmap that will allow us to capture semi transparent images (windows, controls etc.) as transparent. On the contrary the Dot Net way of capturing an image does not capture transparent images as transparent; check the screen shots.

Code Example (VB.Net)

The SCapture class in action:

Public Class Form1

    Private Sub FullScreenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FullScreenButton.Click
        Try
            'Capture the full screen (all monitors) with the cursor.
            Dim img As Image = SCapture.FullScreen(True)
            Me.PictureBox1.Image = img
        Catch ex As Exception
            MessageBox.Show("Failed to capture the full screen!" _
            & Environment.NewLine & ex.Message, "Capture Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub ActiveWindowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActiveWindowButton.Click
        Try
            'Capture the active window with the cursor.
            Dim img As Image = SCapture.ActiveWindow(True)
            Me.PictureBox1.Image = img
        Catch ex As Exception
            MessageBox.Show("Failed to capture the active window!" _
            & Environment.NewLine & ex.Message, "Capture Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub WindowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WindowButton.Click
        Try
            'Capture this window with the cursor.
            Dim img As Image = SCapture.Control(Me, True)
            Me.PictureBox1.Image = img
        Catch ex As Exception
            MessageBox.Show("Failed to capture the window!" _
            & Environment.NewLine & ex.Message, "Capture Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub ControlButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ControlButton.Click
        Try
            'Capture this button by its handle. Include the cursor
            Dim img As Image = SCapture.Control(Me.ControlButton.Handle, True)
            Me.PictureBox1.Image = img
        Catch ex As Exception
            MessageBox.Show("Failed to capture the control!" _
            & Environment.NewLine & ex.Message, "Capture Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub ScreenRectangleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScreenRectangleButton.Click
        Try
            'Capture a screen rectangle area with the cursor.
            Dim img As Image = SCapture.ScreenRectangle(New Rectangle(0, 0, 200, 200), True)
            Me.PictureBox1.Image = img
        Catch ex As Exception
            MessageBox.Show("Failed to capture the control!" _
            & Environment.NewLine & ex.Message, "Capture Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub MonitorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonitorButton.Click
        Try
            'Capture the primary monitor with the cursor.
            Dim img As Image = SCapture.DisplayMonitor(Screen.PrimaryScreen, True)
            Me.PictureBox1.Image = img
        Catch ex As Exception
            MessageBox.Show("Failed to capture the monitor!" _
            & Environment.NewLine & ex.Message, "Capture Error!", _
            MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class

System Requirements

  • Visual Studio
  • Dot Net Framework

Downloads



blog comments powered by Disqus