What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleCapture the image of a Web page
DescriptionThis example shows how to capture the image of a Web page in Visual Basic 6.
KeywordsWeb page, image, capture, Internet Explorer
CategoriesGraphics, Internet, Miscellany
Thanks to James Miller (email address in the code). James says:

Note that the CaptureWindow and CreateBitmapPicture routines are from the Microsoft KB Article Q161299. Comments have been removed but the code is exactly as in the article.

When you click the Copy button, the program gets the position of the WebBrowser and calls function CaptureWindow to get an image of that part of the screen. It copies the image into the Clipboard.

Private Sub cmdCopy_Click()
'  This Copies the Web Page to the Clipboard
    Dim wrkLeft As Long
    Dim wrkTop As Long
    Dim wrkWidth As Long
    Dim wrkHeight As Long
    On Error Resume Next
'  Get the Positions and Widths in Pixels
'  Get Left Position of Actual Web Page - Note the 2nd
' SizableWidth jumps the border
'  of the web page in the the Browser control
    wrkLeft = ScaleX(SizableWidth + brwBrowser.Left + _
        SizableWidth, vbTwips, vbPixels)
'  Get Top Position of Actual Web Page - Note the 2nd
' SizableWidth
    wrkTop = ScaleY(SizableWidth + CaptionHeight + _
        brwBrowser.Top + SizableWidth, vbTwips, vbPixels)
'  Get Width of Actual Web Page - Note the subtraction of 1
' Pixel to account for right margin
    wrkWidth = ScaleX(brwBrowser.Width - SizableWidth, _
        vbTwips, vbPixels) - 1
'  Get Height of Actual Web Page - Note the subtraction of
' 1 Pixel to account for bottom margin
    wrkHeight = ScaleY(brwBrowser.Height - SizableWidth, _
        vbTwips, vbPixels) - 1
'  Capture the Selected Area to the Picture Box
    Set picPicture.Picture = CaptureWindow(hwnd, False, _
        wrkLeft, wrkTop, wrkWidth, wrkHeight)
'  Copy the Picture to the Clipboard
    Clipboard.SetData picPicture.Image
End Sub
Function CaptureWindow makes a bitmap compatible with the target window and copies the window's image into it. It calls CreateBitmapPicture to convert the bitmap handle into a Picture.
Private Function CaptureWindow(ByVal hWndSrc As Long, ByVal _
    Client As Boolean, ByVal LeftSrc As Long, _
        ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal _
            HeightSrc As Long) As Picture
'  Copyright Microsoft - Q161299
    Dim hDCMemory As Long
    Dim hBmp As Long
    Dim HBmpPrev As Long
    Dim r As Long
    Dim hDCSrc As Long
    Dim hPal As Long
    Dim hPalPrev As Long
    Dim RasterCapsScrn As Long
    Dim HasPaletteScrn As Long
    Dim PaletteSizeScrn As Long
    Dim LogPal As LOGPALETTE
    On Error Resume Next
    If (Client = True) Then
        hDCSrc = GetDC(hWndSrc)
        hDCSrc = GetWindowDC(hWndSrc)
    End If
    hDCMemory = CreateCompatibleDC(hDCSrc)
    hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, _
    HBmpPrev = SelectObject(hDCMemory, hBmp)
    RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)
    HasPaletteScrn = RasterCapsScrn And RC_PALETTE
    PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)
    If (HasPaletteScrn <> 0 And PaletteSizeScrn = 256) Then
        LogPal.palVersion = &H300
        LogPal.palNumEntries = 256
        r = GetSystemPaletteEntries(hDCSrc, 0, 256, _
        hPal = CreatePalette(LogPal)
        hPalPrev = SelectPalette(hDCMemory, hPal, 0)
        r = RealizePalette(hDCMemory)
    End If
    r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _
        hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
    hBmp = SelectObject(hDCMemory, HBmpPrev)
    If (HasPaletteScrn <> 0 And PaletteSizeScrn = 256) Then
        hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    End If
    r = DeleteDC(hDCMemory)
    r = ReleaseDC(hWndSrc, hDCSrc)
    Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End Function
Function CreateBitmapPicture initializes some data and uses OleCreatePictureIndirect to make the Picture.
Private Function CreateBitmapPicture(ByVal hBmp As Long, _
    ByVal hPal As Long) As Picture
'  Copyright Microsoft - Q161299
    Dim r As Long
    Dim Pic As PicBmp
    Dim IPic As IPicture
    Dim IID_IDispatch As GUID
    On Error Resume Next
    With IID_IDispatch
        .Data1 = &H20400
        .Data4(0) = &HC0
        .Data4(7) = &H46
    End With
    With Pic
        .Size = Len(Pic)
        .Type = vbPicTypeBitmap
        .hBmp = hBmp
        .hPal = hPal
    End With
    r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, _
    Set CreateBitmapPicture = IPic
End Function
Note that this method takes a snapshot of the Web browser control's image on the screen. If parts of the Web page are scrolled out of view, they are not shown in the image.

Visit James' Web page Daisy Web Tools for a collection of useful Web tools (registration is $29.99/£18.99).

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.