Capture Full Screen - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This is a tutorial on how to take a screen shot of your screen in Visual Basic .NET

For this tutorial, start a new project, add a picturebox, a timer and a button to the form

Change the PictureBox SizeMode property to Zoom

Change the Timer Enabled property to True and change Interval to 1000

Go to the code page and add the following declarations:

Public Shared Image As Image
Public Shared Bounds As Rectangle
Public Shared TimerCount As Integer

Then add the following function to your code:

 Shared Function GetDesktopImage(Optional ByVal Width As Integer = 0, Optional ByVal Height As Integer = 0, Optional ByVal ShowCursor As Boolean = True) As Image
        Dim W As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim H As Integer = Screen.PrimaryScreen.Bounds.Height
        Dim DesktopBitmap As New Bitmap(W, H)
        Dim g As Graphics = Graphics.FromImage(DesktopBitmap)
        g.CopyFromScreen(0, 0, 0, 0, New Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height), CopyPixelOperation.SourceCopy)
        If ShowCursor Then Cursors.Default.Draw(g, New Rectangle(Cursor.Position, New Size(32, 32)))
        g.Dispose()
        If Width = 0 And Height = 0 Then
            Image = DesktopBitmap
            Return DesktopBitmap

        Else
            Dim ScaledBitmap As Image = DesktopBitmap.GetThumbnailImage(Width, Height, Nothing, IntPtr.Zero)
            DesktopBitmap.Dispose()
            Image = ScaledBitmap
            Return ScaledBitmap
        End If
    End Function

Each time the user clicks on the button, the form will be get minimized to enable the user to take a full screen shot of the windows behind.

Add the following Timer Tick event

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        TimerCount += 1

        If TimerCount = 1 And Me.WindowState = FormWindowState.Minimized Then
            PictureBox1.Image = GetDesktopImage()

        End If
        If TimerCount = 2 And Me.WindowState = FormWindowState.Minimized Then
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub

The add the following code that will handle the button click event:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Me.WindowState = FormWindowState.Minimized
        TimerCount = 0
    End Sub

capture full screen in visual basic .net

Download Source Code Here


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2014 - All Rights Reserved.