Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet a window's image using its hWnd in VB .NET
Keywordswindow, image, picture, hWnd, VB.NET
CategoriesGraphics, VB.NET
 
By Ken Tucker.

The GetWindowPicture subroutine uses API functions to get the window's device context (DC) and uses BitBlt to copy the window's image.

Note that this method copies what is currently visible on the window. If the window is partially covered by another window, the result includes the covering window's image.

 
Private Function GetWindowPicture(ByVal hWnd As Integer) As _
    Bitmap
    Dim g As Graphics
    Dim hdcDest As IntPtr
    Dim hdcSrc As Integer
    Dim bm As Bitmap
    Dim r As New RECT()
    Dim w, h As Integer

    GetWindowRect(hWnd, r)

    w = r.Right - r.Left
    h = r.Bottom - r.Top
    bm = New Bitmap(w, h)
    g = g.FromImage(bm)

    hdcSrc = GetWindowDC(hWnd)
    hdcDest = g.GetHdc

    BitBlt(hdcDest.ToInt32, 0, 0, w, h, hdcSrc, 0, 0, _
        SRCCOPY)

    g.ReleaseHdc(hdcDest)
    ReleaseDC(hWnd, hdcSrc)

    Return bm
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated