Skip to main content

VB.NET
Taking & Saving Screenshots (Part 2)

Form1.vb

' AZUL CODING ---------------------------------------
' VB.NET - Taking & Saving Screenshots (Part 2)
' https://youtu.be/ZwWsjXEQLA4


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    If Not NumericUpDown1.Value = 0 Then
        Timer1.Interval = NumericUpDown1.Value * 1000

    Else
        Timer1.Interval = 100

    End If

    Text = "Loading..."
    Enabled = False

    Timer1.Start()

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim ScreenCapture As Bitmap
    Dim graph As Graphics

    ScreenCapture = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Imaging.PixelFormat.Format32bppArgb)
    graph = Graphics.FromImage(ScreenCapture)
    graph.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)

    PictureBox1.BackgroundImage = ScreenCapture

    Text = "Screenshot"
    Enabled = True

    Timer1.Stop()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    PictureBox1.BackgroundImage.Save(My.Computer.FileSystem.SpecialDirectories.MyPictures + "\Screenshots\filename.png")

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Form2.ShowDialog()

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    PictureBox1.BackgroundImage = Nothing

End Sub

Private Sub PictureBox1_BackgroundImageChanged(sender As Object, e As EventArgs) Handles PictureBox1.BackgroundImageChanged

    If Not PictureBox1.BackgroundImage Is Nothing Then
        Button2.Enabled = True

    Else
        Button2.Enabled = False

    End If

End Sub

Enjoying this tutorial?


Form2.vb

' AZUL CODING ---------------------------------------
' VB.NET - Taking & Saving Screenshots (Part 2)
' https://youtu.be/ZwWsjXEQLA4


Dim drag As Boolean
Dim x As Integer
Dim y As Integer

Dim ALoc As New Point(0, 0)
Dim ACur As New Point(0, 0)


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    If Not Form1.NumericUpDown1.Value = 0 Then
        Timer1.Interval = Form1.NumericUpDown1.Value * 1000

    Else
        Timer1.Interval = 100

    End If

    Enabled = False
    Label1.Text = "Loading..."
    Opacity = 100

    Timer1.Start()

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Opacity = 0
    Dim area As Rectangle
    Dim capture As Bitmap
    Dim graph As Graphics

    area = Bounds
    capture = New Bitmap(Bounds.Width, Bounds.Height, Imaging.PixelFormat.Format32bppArgb)
    graph = Graphics.FromImage(capture)
    graph.CopyFromScreen(area.X, area.Y, 0, 0, area.Size, CopyPixelOperation.SourceCopy)
    Form1.PictureBox1.BackgroundImage = capture

    Enabled = True
    Label1.Text = "Click and drag this window to select a region"

    Opacity = 60
    Close()

End Sub


Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
    MoveTimer.Enabled = True
    MoveTimer.Start()
    Definitions()

End Sub

Private Sub Label1_MouseMove(sender As Object, e As MouseEventArgs) Handles Label1.MouseMove

    If drag Then
        Top = Cursor.Position.Y - y
        Left = Cursor.Position.X - x

    End If

End Sub

Private Sub Label1_MouseUp(sender As Object, e As MouseEventArgs) Handles Label1.MouseUp

    MoveTimer.Stop()

    If Location.Y < 0 Then
        Location = New Point(Location.X, 0)

    End If

    Definitions()

End Sub

Private Sub Definitions()
    ALoc = Location
    ACur = Cursor.Position

End Sub

Private Sub MoveTimer_Tick(sender As Object, e As EventArgs) Handles MoveTimer.Tick
    Location = ALoc - ACur + Cursor.Position

End Sub