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:
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports mshtml
<ComVisible(True), ComImport(),
Guid("3050f669-98b5-11cf-bb82-00aa00bdce0b"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceI sIUnknown)> _
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 doing the following:
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?
Here are the links to where I got the C# examples:
http://groups.google.com/groups?hl=e...C%2B%2Bfailure
http://blogs.msdn.com/rfarber/archiv...12/240943.aspx
thanks