A Simple Screen Capture Program without API Calls in VB.NET

This is a really simple basic screen capture utility that needs no API calls. It captures the whole screen and then you can either save it into a file or show it (draw it actually) on the form.

'First, two global variables are declared to load a capture into a bitmap.

Dim bm As Bitmap

'The following is a capture function. A screen copy is made and assigned to a pointer.This is all that is needed to make an image copy of the screen. 

Private Sub btCapture(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton2.Click.

        Dim gr As Graphics
        bm = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Drawing.Imaging.PixelFormat.Format32bppArgb)
        gr = Graphics.FromImage(bm)
        gr.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, NewSize(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))

End Sub

'You may choose to view the captured image. Note the use of the WindowState property.

    Private Sub btShow(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton3.Click
        Dim newGr As Graphics = Me.CreateGraphics()
        Me.WindowState = FormWindowState.Maximized
        newGr.DrawImage(bm, New Point(0, 0))
    End Sub

'Or you can save the image into a file. The following code calls the SaveFileDialog control.

    Private Sub btSave(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton1.Click
        SaveFileDialog1.ShowDialog()
    End Sub

     Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e AsSystem.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
        bm.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub


Similar Articles