Hi all,
I am trying to write a little app that will generate a thumbnail image
of a website. Having searched the web I have found various bits of code
that appear to do the job and have written my app which works but only
sometimes! :(
The problem is - sometimes it returns a black image instead of the nice
screen shot. For example it sometimes works on the msn web site, never
works with the google web site and sometimes does not work with other
sites I try to thumbnail. The app is going to run as a console app- so
the webbrowser is not displayed on the screen. I am using the
AxSHDocVw.AxWebBrowser control to load the html page and waiting until
it has loaded the html.
Any pointers, help would be greatly appriciated
Cheers,
Ben
Here is the code I use to capture
Private Function GetHtmlBody() As IHTMLElement2
Dim wb2 As IWebBrowser2 = CType(m_IE.GetOcx(), IWebBrowser2)
Application.DoEvents()
Dim htmlDocument2 As IHTMLDocument2 = CType(wb2.Document,
IHTMLDocument2)
Application.DoEvents()
Return CType(htmlDocument2.body, IHTMLElement2)
End Function
Private Function CaptureImage() As Bitmap
Dim htmlDocument As IHTMLElement2 = GetHtmlBody()
Application.DoEvents()
Dim W As Integer = htmlDocument.scrollWidth
Dim H As Integer = htmlDocument.scrollHeight
Application.DoEvents()
If Not (m_IE.Height = H + 30) Or Not (m_IE.Width = W + 30) Then
m_IE.Height = H + 30
m_IE.Width = W + 30
End If
' Setup bitmap
Dim bitmapimg As Bitmap = New Bitmap(W, H, PixelFormat.Format24bppRgb)
Dim graphics_imgraphics As Graphics = Graphics.FromImage(bitmapimg)
Dim memdc As IntPtr = graphics_imgraphics.GetHdc()
Dim hbitmap As IntPtr = bitmapimg.GetHbitmap()
SelectObject(memdc, hbitmap)
Const PW_CLIENTONLY As Integer = &H1
Const WM_PRINT As Integer = &H317
Const PRF_NONCLIENT As Integer = &H2
Const PRF_CLIENT As Integer = &H4
Const PRF_ERASEBKGND As Integer = &H8
Const PRF_CHILDREN As Integer = &H10
Const PRF_OWNED As Integer = &H20
Const WM_PAINT As Integer = &HF
Dim rv As Integer
rv = SendMessage(m_IE.Handle, WM_PAINT, memdc, 0)
rv = SendMessage(m_IE.Handle, WM_PRINT, memdc, PRF_CHILDREN +
PRF_CLIENT + PRF_OWNED)
' Save image
Dim bitmapimg2 As Bitmap = Bitmap.FromHbitmap(hbitmap)
' Cleanup
DeleteObject(hbitmap)
graphics_imgraphics.ReleaseHdc(memdc)
graphics_imgraphics.Dispose()
bitmapimg.Dispose()
Return bitmapimg2
End Function