|
|
Title | Capture an image of what's displayed on a WebBrowser control |
Keywords | WebBrowser, image, capture, Web page, URL |
Categories | Controls |
|
|
Use BitBlt to copy an image from the WebBrowser's device context (DC) to a PictureBox.
|
|
Private Sub mnuFileCaptureImage_Click()
Dim web_hwnd As Long
Dim web_dc As Long
' Prepare the display form.
frmPicture.Picture1.Move 0, 0, ScaleWidth, ScaleHeight
frmPicture.Width = frmPicture.Picture1.Width + _
frmPicture.Width - frmPicture.ScaleWidth
frmPicture.Height = frmPicture.Picture1.Height + _
frmPicture.Height - frmPicture.ScaleHeight
frmPicture.Picture1.AutoRedraw = True
frmPicture.Picture1.ScaleMode = vbPixels
' Find the WebBrowser's hWnd.
web_hwnd = FindHwnd(Me.hwnd, "Shell Embedding")
' Get the corresponding DC.
web_dc = GetDC(web_hwnd)
' Copy the image from the DC to Picture1.
BitBlt frmPicture.Picture1.hDC, _
0, 0, _
frmPicture.Picture1.ScaleWidth, _
frmPicture.Picture1.ScaleHeight, _
web_dc, _
0, 0, SRCCOPY
' Display the results.
frmPicture.Show
End Sub
|
|
Unfortunately the WebBrowser's hWnd property doesn't correctly return a window handle so you need to do a little work to get it. The FindHwnd function searches a form's children for a control with a given class name ("Shell Embedding" for the WebBrowser).
|
|
' Find the child window with the indicated class name.
Public Function FindHwnd(ByVal container_hwnd As Long, _
ByVal target_class As String) As Long
Dim child_hwnd As Long
Dim class_name As String * 256
child_hwnd = GetWindow(container_hwnd, GW_CHILD)
Do
' See if this is the target class.
GetClassName child_hwnd, class_name, 256
If Left$(class_name, Len(target_class)) = _
target_class Then
' This is it.
FindHwnd = child_hwnd
Exit Do
End If
' Get the next child.
child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
Loop While child_hwnd <> 0
End Function
|
|
Note also that this method only captures what's actually visible on the WebBrowser control, not the whole Web page. If the WebBrowser is covered by another window, a piece of the other window is captured.
|
|
|
|
|
|