by DanDaMan (2 Submissions)
Category: Graphics
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating:
(8 Votes)
Seen many posts using api for printscreen keypress, but what about the info on clipboard? Seen others that copy clipboard to temp var, and set back after, but why waste resources and processing when you can do it the right way? Also seen classes using DIB, wayyy slow. This = best way! =D
This uses two tricks that arent very known, so here you go! When using GetDC(0) as our source, we actually capture the screen, and when using picture.image=picture.picture.
Assumes
Always make sure the Picturebox has AutoRedraw enabled. Create a Command1, and in it call ScreenShot()
Code Returns
A screenshot in the app folder called screenshot.bmp
API DeclarationsBitblt and GetDC
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetDC Lib "USER32" (ByVal hwnd As Long) As Long
Private Sub ScreenShot()
'Important Precursors
'All these can be set in form load
'The Picturebox can also be Visible=False
'and this will still work
Picture1.Width = Screen.Width
Picture1.Height = Screen.Height
Picture1.AutoRedraw = True
BitBlt Picture1.hDC, 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY, GetDC(0), 0, 0, vbSrcCopy
Picture1.Picture = Picture1.Image
SavePicture Picture1.Picture, App.Path & "/screen.bmp"
End Sub