Hi,
I am having problems with using the DrawToDC of the
MSHTML.iHTMLElementRender in a VB.net application. For some reason I am
getting a "catastrophic error". I am basing the code on c# examples, and I
am not sure what exactly I am doing wrong. From the C# posts it seems that
the drawtodc has a bug and you need to redifine it, so i followed their
advice and specified an interface as below but without any luck:
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports mshtml
_
Interface IHTMLElementRender
Sub DrawToDC(<[In]()> ByVal hDC As IntPtr)
Sub SetDocumentPrinter(<[In](), MarshalAs(UnmanagedType.BStr)> ByVal
bstrPrinterName As String, <[In]()> ByVal hDC As IntPtr)
End Interface 'IHTMLElementRender
On the main form of the application I am have the folowing code when a button is clicked:
Dim objMSHTML As HTMLDocument
Dim objDocument As IHTMLDocument2
Dim ips As IPersistStreamInit
objMSHTML = New HTMLDocument()
ips = DirectCast(objMSHTML, IPersistStreamInit)
ips.InitNew()
objDocument =
objMSHTML.createDocumentFromUrl("http://www.google.com", String.Empty)
Do Until objDocument.readyState = "complete"
Application.DoEvents()
Loop
MsgBox(objDocument.body.outerHTML)
Dim bodyElement As IHTMLElement
Dim render As IHTMLElementRender
If objDocument.body.outerHTML <> Nothing Then
bodyElement = objDocument.body
render = bodyElement
Dim img As New Bitmap(600, 400)
Dim g As Graphics = Graphics.FromImage(img)
Dim memDC As IntPtr
memDC = g.GetHdc()
Try
render.DrawToDC(memDC)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
It seems like I am getting the HTML document just fine, is just that when I
try to use DrawToDC to get the application to print/send to the DC i Created
in memory, it causes the error. Any ideas on what I am doing wrong?
From the dotnet newsgroups I was told I am not hosting MSHTMl fully and that might be the cause . I was told that I should use either the internetexplorer/webbrowser controls or the MSHTMl fully. I cannot find any source on the web on how to do this in vb.net I tried the following but this also failed.
Dim browser As New SHDocVw.InternetExplorer()
browser.Navigate("http://www.google.com")
Do Until browser.ReadyState = 4
Application.DoEvents()
Loop
Dim objDocument As IHTMLDocument2 = browser.Document
MsgBox(objDocument.body.outerHTML)
Dim bodyElement As IHTMLElement = objDocument.body
Dim render As IHTMLElementRender = bodyElement
Dim img As New Bitmap(600, 400)
Dim g As Graphics = Graphics.FromImage(img)
Dim memDC As IntPtr
memDC = g.GetHdc()
Try
render.DrawToDC(memDC)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Here are the links to where I got the C# examples:
http://groups.google.com/groups?hl=en&lr=&threadm=a299f931.0406011058.43b92799%40posting.google.com&rnum=5&prev=/groups%3Fnum%3D100%26hl%3Den%26lr%3D%26q%3DDrawToDC%2B%2Bfailure
http://blogs.msdn.com/rfarber/archive/2004/10/12/240943.aspx
thanks