Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / Visual Basic

A Screen Capture Utility

Rate me:
Please Sign up or sign in to vote.
4.25/5 (12 votes)
19 Oct 2007CPOL2 min read 57.6K   3.5K   54  
A screen capture utility for developers as well as generic users.
'//Written by KEN (Minjian Huang).
'//(c) 2007 Management strategies Ltd 
'//http://www.ms-strategies.com/

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Imaging

'hotkey
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Threading
Imports System.IO


Public Class Main
    '======================================================
    Public Function SaveScreen(ByVal theFile As String) As Boolean

        Try
            SendKeys.Send("{PRTSC}") '<alt + printscreen>
            Application.DoEvents()

            Dim data As IDataObject = Clipboard.GetDataObject()

            If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
                Dim bmp As Bitmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
                bmp.Save(theFile, Imaging.ImageFormat.Png)
            End If
            Clipboard.SetDataObject(0) 'save memory by removing the image from the clipboard
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function
    '======================================================
    Private Sub Main_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Show_parameters_on_screen()
        Debug.Print(hotVkey_lst.Text)
    End Sub
    '======================================================
    Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        load_default_if_needed()
        Load_config()
        Show_parameters_on_screen()
        inVisibleHotkey.Hotkey_Load()
    End Sub
    '======================================================
    Sub Show_parameters_on_screen()
        'show any changes to the object on screen

        Dim key_Tips As String
        Save_btn.Enabled = False

        'destination
        Dir_path_txt.Text = Dir_Path
        Prefix_fn_txt.Text = Prefix_fn

        'hotkey
        key_Tips = map_Modifier_to_Visual(hotfsModifiers, Alt_chk.Checked, Ctrl_chk.Checked, Shift_chk.Checked)
        map_vkey_to_Visual(hotVkey, hotVkey_lst.Text)

        'hotkey tips
        key_Tips += hotVkey_lst.Text
        Debug.Print(hotVkey_lst.Text)
        Debug.Print(key_Tips)
        Hotkey_tip.Text = "press " & key_Tips & " to capture"

        'format
        Subfix_fn_lst.Text = map_Format_to_Visual(Subfix_fn)

        'area
        Area_tab.Controls("Area_rad_" & Area).Select()

    End Sub
    '======================================================
    Function Check_empty_fields() As String
        'this routine is not finished, it will be used to check the input fields validation.

        If Trim(Dir_path_txt.Text) = "" Then
            Return "Dir_path_txt"
        ElseIf Trim(Prefix_fn_txt.Text) = "" Or (Not isFilename(Prefix_fn_txt.Text)) Then
            Return "Prefix_fn_txt"
        Else
            Return ""
        End If
    End Function
    '======================================================
    Sub Read_parameters_from_screen()
        'save the changes of the objects on screen into the variables.

        Save_btn.Enabled = False
        'destination
        Dir_Path = Trim(Dir_path_txt.Text)
        Prefix_fn = Trim(Prefix_fn_txt.Text)

        'hotkey
        map_Visual_to_Modifier(hotfsModifiers, Alt_chk.Checked, Ctrl_chk.Checked, Shift_chk.Checked)
        Debug.Print(hotfsModifiers)
        map_Visual_to_vkey(hotVkey, hotVkey_lst.Text)

        'format
        Subfix_fn = map_Visual_to_Format(Subfix_fn_lst.Text)

        'area
        Area = map_Visual_to_Area()
    End Sub
    '======================================================
    Function map_Visual_to_Area() As Integer
        If Area_rad_1.Checked Then
            Return 1
        ElseIf Area_rad_2.Checked Then
            Return 2
        ElseIf Area_rad_3.Checked Then
            Return 3
        Else
            Stop
        End If
    End Function
    '======================================================
    Sub Load_config()
        'load config.ini into variables

        get_Startup_path()
        get_Config(Startup_path & "config.ini")
    End Sub
    '======================================================
    Sub Save_config()
        'save variables into config.ini

        set_Config(Startup_path & "config.ini")
    End Sub
    '======================================================
    Public Sub Cap_screen(ByVal Filename As String, ByVal Subfix_fn As String)
        'everytime this routine is called, it will capture the current desktop to a screenshot file.

        Dim sc As ScreenCapture = New ScreenCapture()
        Select Case Subfix_fn
            Case "jpg"
                sc.CaptureScreenToFile(Filename, ImageFormat.Jpeg)
            Case "gif"
                sc.CaptureScreenToFile(Filename, ImageFormat.Gif)
            Case "bmp"
                sc.CaptureScreenToFile(Filename, ImageFormat.Bmp)
            Case "tif"
                sc.CaptureScreenToFile(Filename, ImageFormat.Tiff)
            Case "png"
                sc.CaptureScreenToFile(Filename, ImageFormat.Png)
        End Select
    End Sub
    '======================================================
    Public Sub HotKey_Press()
        'everytime the hotkey is pressed, this routine will be called

        Debug.Print("hotkey!")
        If Area = 3 Then
            inVisibleCapWin.PrepareCap()
        Else
            Dim Filename As String
            Filename = makeup_Filename(Scr_num, Dir_Path, Prefix_fn, Subfix_fn)
            Cap_screen(Filename, Subfix_fn)
        End If
    End Sub
    '======================================================
    Private Sub Cap_Now_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cap_Now_btn.Click
        'capture the screen by clicking the cap button

        Me.WindowState = FormWindowState.Minimized
        If Area = 3 Then
            inVisibleCapWin.PrepareCap(False)
        Else
            Dim Filename As String
            Filename = makeup_Filename(Scr_num, Dir_Path, Prefix_fn, Subfix_fn)
            Cap_screen(Filename, Subfix_fn)
            waitForCapFinished()
        End If
    End Sub
    '======================================================
    Public Sub waitForCapFinished()
        'delay for the screen capture, then restore the main window.
        Timer1.Enabled = True
    End Sub
    '======================================================
    Private Sub Dir_path_txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dir_path_txt.TextChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Dir_path_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dir_path_btn.Click
        Dim Selected_path As String
        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Selected_path = Standardize_path(FolderBrowserDialog1.SelectedPath)
            Dir_path_txt.Text = Selected_path
        End If
    End Sub
    '======================================================
    Private Sub Prefix_fn_txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Prefix_fn_txt.TextChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        'save the setting into the variables and the config file

        'check the validation of the input
        Dim Check_fields As String
        Check_fields = Check_empty_fields()
        If Not Check_fields = "" Then
            MsgBox("This value is not acceptable!")
            Destination_tab.Controls(Check_fields).Select()
            Exit Sub
        End If

        Read_parameters_from_screen()
        Save_config()
        'if renew the hotkey changed if necessary
        If hotVkey_reg <> hotVkey Or hotfsModifiers_reg <> hotfsModifiers Then
            inVisibleHotkey.hotkey_unload()
            inVisibleHotkey.Hotkey_Load()
        End If
        Me.WindowState = FormWindowState.Minimized
    End Sub
    '======================================================
    Private Sub Cancel_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        Me.WindowState = FormWindowState.Minimized
        'cancel any changes
        Show_parameters_on_screen()
    End Sub
    '======================================================
    Private Sub Alt_chk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Alt_chk.CheckedChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Ctrl_chk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ctrl_chk.CheckedChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Shift_chk_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Shift_chk.CheckedChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub hotVkey_lst_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hotVkey_lst.SelectedIndexChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Hide_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hide_btn.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub
    '======================================================
    Private Sub Exit_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit_btn.Click
        If MsgBox("Do you really want to exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Me.Close()
        End If
    End Sub
    '======================================================
    Private Sub Subfix_fn_lst_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Subfix_fn_lst.SelectedIndexChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Area_rad_1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Area_rad_1.CheckedChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Area_rad_2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Area_rad_2.CheckedChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Area_rad_3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Area_rad_3.CheckedChanged
        Save_btn.Enabled = True
    End Sub
    '======================================================
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.WindowState = FormWindowState.Normal
        Timer1.Enabled = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Written by KEN (Minjian Huang)." & vbCrLf & "(c) 2007 Management strategies Ltd" & vbCrLf & "http://www.ms-strategies.com/")
    End Sub
End Class


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions