Results 1 to 6 of 6

Thread: Take screenshot of external application

Threaded View

  1. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Take screenshot of external application

    Quote Originally Posted by csKanna View Post
    a. if there are other windows on top of that application, it takes the screenshot of that top most window.
    The PrintWindow API can capture windows even if they are behind other windows or off to the sides of the desktop area.
    One minor drawback is PrintWindow doesn't support opacity so captured windows are always solid.

    Code:
    Public Class Form1
    ' Tested on: WinXP-32 and Win7-64.
        Public Structure RECT
            Public left As Int32
            Public top As Int32
            Public right As Int32
            Public bottom As Int32
        End Structure
        Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Int32
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        Private Declare Function PrintWindow Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As Int32) As Int32
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            ' get handle of app to capture by the titlebar text
            Dim hWnd As IntPtr = FindWindow(Nothing, "Calculator") '<<< WINDOWS CALCULATOR
    
            If hWnd = IntPtr.Zero Then
                MessageBox.Show("Window not found!", "No Window Match", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                ' Capture app to Bitmap
                Dim bmp As Bitmap = Capture_hWndToBitmap(hWnd)
                If bmp IsNot Nothing Then
                    Try
                        ' save bitmap to a public folder
                        bmp.Save("C:\Users\Ed\Pictures\TEST_CAPTURE.png", Imaging.ImageFormat.Png)                    
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                    bmp.Dispose()
                Else
                    MessageBox.Show("Capture failed!", "Capture Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        End Sub
    
        ' Capture window by handle to bitmap
        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)
                Dim hDC As IntPtr = g.GetHdc()
                Dim pwRet As Integer = PrintWindow(WindowHandle, hDC, 0)
                g.ReleaseHdc(hDC)
                If pwRet = 0 Then ' printwindow failed!
                    Return Nothing
                Else
                    Return bmp
                End If
            Catch
                Return Nothing ' something went wrong!
            End Try
        End Function
    
    End Class
    I had posted something similar yesterday but removed it because I was getting an error when run under Win7, turns out I didn't have permission to write the file to the drive/folder I selected!
    Last edited by Edgemeal; Apr 9th, 2011 at 03:57 AM. Reason: Check PrintWindow return value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width