Re: Browser Contents to Picture

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Doug van Vianen" <courses@xxxxxxx> wrote in message news:vcjMg.520447$iF6.81024@xxxxxxxxxxx

Hi Mike, Thank you for the two methods. The first one sort of
works but only saves a part of the actual browser window (the
top and bottom are blanked out???). The second method works
fine.

I had an idea that the first method would not work properly because, as I said in my post, that specific method often has problem when the object you are copying (the Form in this case) contains controls which themselves can act as containers. However, it is possible to get around that problem by using exactly the same method on the Browser control itself instead of on the Form. In that way you gain some extra functionality as well. Firstly, this method does not require the WebBrowser (or the Form) to be actually visible on the display and secondly you can copy the entire Browser window, including the parts that you would normally need to scroll to see when the window is taller than the display. As I said earlier, I never use Browser controls myself and I know almost nothing about them, so the simple example in the following code requires you to wait a few seconds (or longer if you're not on broadband) for the browser window to fully load before you click the button to copy it as a bitmap. I'm sure there are ways that you can ask the Browser to tell you when the page has fully loaded though, and if so you will be able to modify the code accordingly. Paste the code into a VB Form containing a WebBrowser control and a Picture Box and a Command Button. You can leave the Form at its initial default size if you wish. Run the program and wait ten seconds or so until the Browser loads the page and then click the Command Button. You should end up with a bmp file called "webpic1.bmp" in your main c:\ drive (I've hard coded the path just for simplicity, so you'll probably want to change it). Check out the bmp file and it should show the entire page. Note that the WebBrowser is visible in this example (so that you can see what is happening) but you can cause it to effectively be invisible by removing the comment form the "WebBrowser1.Left = Me.ScaleWidth" line. Check it out and let me know how it works on your own system.

Mike Williams
MVP Visual Basic

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" (ByVal hwnd As Long, _
ByVal lpClassName As String, ByVal nMaxCount As Long) _
As Long
Private Const WM_PAINT = &HF
Private Const WM_PRINT = &H317
Private Const PRF_CHILDREN = &H10&
Private Const PRF_CLIENT = &H4&
Private Const PRF_OWNED = &H20&
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Sub Command1_Click()
Dim myWindow As Long, childWindow As Long
Dim myClass As String, className As String * 256
Picture1.Width = WebBrowser1.Width
Picture1.Height = WebBrowser1.Height
Picture1.Cls
myClass = "Shell Embedding"
childWindow = GetWindow(Me.hwnd, GW_CHILD)
Do
GetClassName childWindow, className, 256
If Left$(className, Len(myClass)) = myClass Then
myWindow = childWindow
Exit Do
End If
childWindow = GetWindow(childWindow, GW_HWNDNEXT)
Loop While childWindow <> 0
If myWindow <> 0 Then
SendMessage myWindow, WM_PAINT, Picture1.hDC, 0
SendMessage myWindow, WM_PRINT, Picture1.hDC, _
PRF_CHILDREN + PRF_CLIENT + PRF_OWNED
Picture1.Picture = Picture1.Image
SavePicture Picture1.Picture, ("c:\webpic1.bmp")
End If
End Sub

Private Sub Form_Load()
Picture1.Visible = False
Picture1.BorderStyle = vbBSNone
Picture1.AutoRedraw = True
'WebBrowser1.Left = Me.ScaleWidth
WebBrowser1.Width = Me.ScaleX(800, vbPixels, Me.ScaleMode)
WebBrowser1.Height = Me.ScaleX(1100, vbPixels, Me.ScaleMode)
WebBrowser1.Navigate2 "http://www.yahoo.co.uk";
End Sub






.


Quantcast