Help implementing IViewObject.Draw (COM – Unmanaged code) in VB.net

  • 12 years ago

    I am trying to write an HTML DOM object (the rendered screen in memory) to a screen device context using IViewObject.Draw. 

    I am using code posted on developer fusion by Alan Dean’s thread titled “Generate an Image of a Web Page” But I need to implement the writing of, the rendered HTML document, from memory to a DC, (Device Context), using a different method than IHTMLElement.DrawToDC because it has been suggested to me that using the DrawToDc function doesn’t always work properly; it can leave out stuff.  IViewObject.Draw on the other hand will pick up all elements of the memory rendered HTML screen. 

    So the first part of my problem is the parameter list required by IViewObject.Draw: unfortunately Microsoft Doc (I am not a C++ programmer/COM Objects) does not spell out simply and clearly what all of the parameters mean and which ones would be required to write from the dimensioned “element” below to the Device Context below “PointerToDC” using dimensioned “render”

    The reason for doing it this way is eloquently described by Mr. Dean in his article “Generate an Image of a Web Page”

    Try
    Dim document As mshtml.IHTMLDocument2 = DirectCast(Me.WebBrowser1.Document.DomDocument, mshtml.IHTMLDocument2)
    Dim element As mshtml.IHTMLElement2 = DirectCast(document.body, mshtml.IHTMLElement2)
    Dim render As IHTMLElementRender = DirectCast(element, IHTMLElementRender)

    Dim GraphicsObject As Graphics = Me.PictureBox1.CreateGraphics
    Dim PointerToGraphics As IntPtr = GraphicsObject.GetHdc
    Dim PointerToDC As IntPtr = CreateCompatibleDC(IntPtr.Zero)
    Dim PointerToBitmap As IntPtr = CreateCompatibleBitmap(PointerToGraphics, Me.WebBrowser1.Width, Me.WebBrowser1.Height)

    SelectObject(PointerToDC, PointerToBitmap)
    Render.DrawToDC(pointerToDC)
    (The code that follows simply takes the written screen from the Device Context and creates a Thumbnail for it.)


    HERE IS THE IViewObject.Draw Subroutine and it’s parameter list (my problem) in my vb.net code:  This is where I need initial help

    <Guid("0000010D-0000-0000-C000-000000000046")> <InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), ComVisible(True), ComImport()> _
    Interface IViewObject

        Sub Draw(ByVal dwDrawAspect As UInteger, _
        ByVal lindex As Integer, _
        ByVal pvAspect As UInteger, _
        ByVal ptd() As Microsoft.VisualStudio.OLE.Interop.DVTARGETDEVICE, _
        ByVal hdcTargetDev As UInteger, ByVal hdcDraw As UInteger, _
        ByVal lprcBounds() As Microsoft.VisualStudio.OLE.Interop.RECTL, _
        ByVal lprcWBounds() As Microsoft.VisualStudio.OLE.Interop.RECTL, _
        ByVal pContinue As Microsoft.VisualStudio.OLE.Interop.IContinue)

    End Interface

    Secondarily I am also having a problem figuring out how to directcast IviewObject.Draw to Dim element as IHTMLElement2 getting invalid cast- Or use IVewObject.Draw to write element to the DC. 

    Below is the new code:

    Try
    Dim document As mshtml.IHTMLDocument2 = DirectCast(Me.WebBrowser1.Document.DomDocument, mshtml.IHTMLDocument2)
    Dim element As mshtml.IHTMLElement2 = DirectCast(document.body, mshtml.IHTMLElement2)
    'Dim render As IHTMLElementRender = DirectCast(element, IHTMLElementRender)
    Dim Drawrender As IViewObject = DirectCast(element, IViewObject)

    Dim GraphicsObject As Graphics = Me.PictureBox1.CreateGraphics
    Dim PointerToGraphics As IntPtr = GraphicsObject.GetHdc
    Dim PointerToDC As IntPtr = CreateCompatibleDC(IntPtr.Zero)
    Dim PointerToBitmap As IntPtr = CreateCompatibleBitmap(PointerToGraphics, Me.WebBrowser1.Width, Me.WebBrowser1.Height)

    SelectObject(PointerToDC, PointerToBitmap)
    Render.DrawToDC(PointerToDC)
    Drawrender.Draw(?????????  Not

    Help Greatly Appreciated.  Greg

     

     

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”