Home > Code > VB.NET > HotKey to take and save screenshots in the background (BMP high resolution format)

HotKey to take and save screenshots in the background (BMP high resolution format)

by Anthony M   on Sep 24, 2017   Category: VB.NET   |  Views: 1383    |  Points: 25   |  Starter 

This program runs in the background and detects a hotkey (Alt+F1), when pressed it will take and save a numbered screenshot to a folder in the same directory as the application (screenshots)

couple of notes: will auto create screenshots folder if it is missing. The application will auto number files so it doesn't overwrite. This application can run minimized or in the background.

Imports System.Runtime.InteropServices
Imports System.IO.Path
Imports System.Drawing.Drawing2D

Public Class Form1

' Auto Screenshot Saver.
' Wait for user to press Alt+F1, then takes and saves a numbered screenshot to the screenshots\ folder.
' Notes - It takes it in high quality, but all depends on graphics card, it may come out slightly blurry.

Public Const MOD_ALT As Integer = &H1 'Alt key
Public Const WM_HOTKEY As Integer = &H312 ' does nothing at the moment

Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr,
ByVal id As Integer, ByVal fsModifiers As Integer,
ByVal vk As Integer) As Integer
End Function

Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr,
ByVal id As Integer) As Integer
End Function

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load

ListBox1.Items.Add("Waiting For Input (Alt+F1)")

RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.F1)
End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
If id.ToString = 100 Then
ListBox1.Items.Add("Keys Pressed: Grabbing And Saving Screenshot")
End If
End If
End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
UnregisterHotKey(Me.Handle, 100)
UnregisterHotKey(Me.Handle, 200)
End Sub

Private Function TakeScreenShot() As Bitmap
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
screenGrab.SetResolution(2400, 2400)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CompositingQuality = CompositingQuality.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.SmoothingMode = SmoothingMode.HighQuality
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)

Return screenGrab
End Function

Public Sub scshot()
Dim pic As Bitmap = TakeScreenShot()
Dim x As Integer = 0
If Not FileIO.FileSystem.DirectoryExists("screenshots") Then
End If
While FileIO.FileSystem.FileExists("screenshots\screengrab_" & x & ".bmp")
x = x + 1
End While
pic.Save("screenshots\screengrab_" & x & ".bmp")
ListBox1.Items.Add("Saved: screenshots\" & "screengrab_" & x & ".bmp")
ListBox1.Items.Add("Waiting For Input (Alt+F1)")
End Sub

End Class

Post Code  |  Code Snippet Home

User Responses

No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts