Screenshot of IntPtr/Handle only works wehn my application is active

  • Thread starter Thread starter valaki2018
  • Start date Start date
V

valaki2018

Guest
I have a Problem with some Functions. When I try to call the function it will not work when my application is minimized or inactive.

This Function should take a screenshot of the IntPtr I pass it.

But it is only working if my application is activated.

Is there an other way to take screenshots from a hWnd or IntPtr?



Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Int32


Private Declare Function PrintWindow Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As Int32) As Int32
Private Function Capture_hWndToBitmap(ByVal WindowHandle As IntPtr) As Bitmap
Try
Dim WinRect As RECT
GetWindowRect(WindowHandle, WinRect)
Dim bmp As Bitmap = New Bitmap(WinRect.right - WinRect.left, WinRect.bottom - WinRect.top)
Dim g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.Transparent)
Dim hDC As IntPtr = g.GetHdc()
Dim pwRet As Integer = PrintWindow(WindowHandle, hDC, 0)
g.ReleaseHdc(hDC)
If pwRet = 0 Then
Return Nothing
Else
Return bmp
End If
Catch
Return Nothing
End Try
End Function

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Image = Nothing
'If You don't clear the previous image it will just give you the effect it frozen.

PictureBox1.Image = Capture_hWndToBitmap(AnotherApplicationsHandle)
End Sub

Continue reading...
 

Similar threads

Back
Top