ARTICLE

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

Posted by Steve Youn Articles | Visual Basic 2010 October 11, 2010
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.
 
Reader Level:

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) Handles Button2.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, New Size(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) Handles Button3.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) Handles Button1.Click
        SaveFileDialog1.ShowDialog()
    End Sub

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

 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor