I would like to know the code to capture the current desktop screen and paste the picture in a picturebox in my form. Does anyone know how to do this? Help me if you can please! :confused:
Printable View
I would like to know the code to capture the current desktop screen and paste the picture in a picturebox in my form. Does anyone know how to do this? Help me if you can please! :confused:
Best regardsVB Code:
Private Declare Function GetDesktopWindow _ Lib "user32" () As Long Private Declare Function GetDC _ Lib "user32" ( _ ByVal hwnd As Long) As Long Private Declare Function ReleaseDC _ Lib "user32" ( _ ByVal hwnd As Long, _ ByVal hdc As Long) As Long 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 Sub CaptureDesktop() Dim hWindow As Long Dim hDContext As Long [b]Picture1[/b].AutoRedraw = True hWindow = GetDesktopWindow() hDContext = GetDC(hWindow) BitBlt [b]Picture1[/b].hDC, 0, 0, Screen.Width \ Screen.TwipsPerPixelsX, _ Screen.Height \ Screen.TwipsPerPixelsY, _ hDContext, 0, 0, vbSrcCopy [b]Picture1[/b].Refresh [b]Picture1[/b].Picture = [b]Picture1[/b].Image ReleaseDC hWindow, hDContext End Sub
I just tried that code.............
it said..........
Member or Data Member not found
and it highlighted
.TwipsPerPixelsX
can you help me?
Sorry! I have typed it wrong. That happens sometimes when you type directly in the post instead of doing it in VB and then copy and paste the code :)
Here's the correct procedure:Best regardsVB Code:
Private Sub CaptureDesktop() Dim hWindow As Long Dim hDContext As Long Picture1.AutoRedraw = True hWindow = GetDesktopWindow() hDContext = GetDC(hWindow) BitBlt Picture1.hdc, 0, 0, _ Screen.Width \ Screen.TwipsPerPixelX, _ Screen.Height \ Screen.TwipsPerPixelY, _ hDContext, 0, 0, vbSrcCopy Picture1.Refresh Picture1.Picture = Picture1.Image ReleaseDC hWindow, hDContext End Sub
I got it to work by taking out the Twips part.. What did that part of the code do anyway? Thanx!
It re-calculates the Twips mesurement used by VB to pixels that you have to use in API calls. You should put them back otherwise this code will take much longer then necassary to call.
How would I make a screenshot of a specific window?
I've got the classname (or handle) of the window
Use the FindWindow to get the hWnd of the window. Then use the above code from the GetDC call (and send the handle of the window).