How to take Screenshot ( Same as Printscreen, just faster) With Hotkey! 08-18-2013, 12:08 PM
#1
I'm was working on an All-in-one application for my laptop since I'm starting school tomorrow and i wanted to share the code here just in case anyone was interested.
The hotkey works outside of the focused window, meaning it will work with whatever you're using.
[code=vb]
Public Class Form1
Public ImageData As Image
Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer
Public TakeScreenshotViaHotkey As Boolean = True
Sub TakeScreenShot()
Dim Bounds As Rectangle
Dim CapturedImg As System.Drawing.Bitmap
Dim GFX As Graphics
Bounds = Screen.PrimaryScreen.Bounds
CapturedImg = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
GFX = Graphics.FromImage(CapturedImg)
GFX.CopyFromScreen(Bounds.X, Bounds.Y, 0, 0, Bounds.Size, CopyPixelOperation.SourceCopy)
ImageData = CapturedImg
SaveScreenShot()
End Sub
Sub SaveScreenShot()
'Start of BMP format
SaveFileDialog1.DefaultExt = "*.bmp"
SaveFileDialog1.Filter = "Bitmap Images|*.bmp"
SaveFileDialog1.ShowDialog()
Try
ImageData.Save(SaveFileDialog1.FileName)
Catch ex As Exception
MessageBox.Show("An error has occured while saving" & vbNewLine & "And i don't know what's wrong.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Bouton1_Click(sender As Object, e As EventArgs) Handles Bouton1.Click
TakeScreenShot()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Update_Stuff.Tick ' When the printscreen button is pressed it will run the "TakeScreenshot" sub.
If TakeScreenshotViaHotkey = False Then
If GetKeyPress(Keys.PrintScreen) Then
TakeScreenShot()
End If
End If
End Sub
Private Sub Hotkey_Checkbox_Click(sender As Object, e As EventArgs) Handles Hotkey_Checkbox.Click 'Enables and disable the ability to use the hotkey.
If Hotkey_Checkbox.Checked = True Then
TakeScreenshotViaHotkey = True
Else
TakeScreenshotViaHotkey = False
End If
End Sub
End Class
[/code]
Credits:
Not 100 % mine.
The hotkey works outside of the focused window, meaning it will work with whatever you're using.
[code=vb]
Public Class Form1
Public ImageData As Image
Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer
Public TakeScreenshotViaHotkey As Boolean = True
Sub TakeScreenShot()
Dim Bounds As Rectangle
Dim CapturedImg As System.Drawing.Bitmap
Dim GFX As Graphics
Bounds = Screen.PrimaryScreen.Bounds
CapturedImg = New System.Drawing.Bitmap(Bounds.Width, Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
GFX = Graphics.FromImage(CapturedImg)
GFX.CopyFromScreen(Bounds.X, Bounds.Y, 0, 0, Bounds.Size, CopyPixelOperation.SourceCopy)
ImageData = CapturedImg
SaveScreenShot()
End Sub
Sub SaveScreenShot()
'Start of BMP format
SaveFileDialog1.DefaultExt = "*.bmp"
SaveFileDialog1.Filter = "Bitmap Images|*.bmp"
SaveFileDialog1.ShowDialog()
Try
ImageData.Save(SaveFileDialog1.FileName)
Catch ex As Exception
MessageBox.Show("An error has occured while saving" & vbNewLine & "And i don't know what's wrong.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Bouton1_Click(sender As Object, e As EventArgs) Handles Bouton1.Click
TakeScreenShot()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Update_Stuff.Tick ' When the printscreen button is pressed it will run the "TakeScreenshot" sub.
If TakeScreenshotViaHotkey = False Then
If GetKeyPress(Keys.PrintScreen) Then
TakeScreenShot()
End If
End If
End Sub
Private Sub Hotkey_Checkbox_Click(sender As Object, e As EventArgs) Handles Hotkey_Checkbox.Click 'Enables and disable the ability to use the hotkey.
If Hotkey_Checkbox.Checked = True Then
TakeScreenshotViaHotkey = True
Else
TakeScreenshotViaHotkey = False
End If
End Sub
End Class
[/code]
Credits:
Not 100 % mine.