Thumbnail an html page directly from IE programmatically?

I would like to grab an HTML page from a WebBrowser control and thumbnail it to a picturebox control.  Anyone out there know how to do this   

The only way I can see to do this is to use shareware or a commercial product to convert an html page to a bmp, external to the program,  save it on disk and then use GetThumbnailImage to thumbnail it.

Thanks


Answer this question

Thumbnail an html page directly from IE programmatically?

  • dimkaz

    Were you able to get a screenshot of a full document, not just what is viewable on the screen   So if you have a 2 page HTML document, is there a way to get the entire thing

    Thanks.

    -- Aaron

  • Clemens Kolbitsch

    Excellent!

    I just read your reply and if this works it dresses up my application and solves a big problem for me since I should be able to do it programmatically!!!  It may take a few seconds to screen capture it; write it to disk as a bmp; thumbnail it and then place the thumbnail where I want it on a form but that’s ok because I can preload it for a driver form!!!!

    Thanks Justin, I will definitely let you know the outcome

    GregC

  • cumars

    Thanks,  I'll give it a shot and let you know.  Greg
  • bubbly

    Pretty close to the end.  Will try the StretchBlt function 

    Thanks Again GregC


    Public Class SetCaptureImagePointers

        Public g1 As Graphics = MYform1.CreateGraphics()
        Public MyImage = New Bitmap(85, 85, g1)
        Public g2 As Graphics = Graphics.FromImage(MyImage)
        Public dc1 As IntPtr = g1.GetHdc()
        Public dc2 As IntPtr = g2.GetHdc()

        <System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
        Public Shared Function BitBlt(ByVal hdcDest As IntPtr, _
        ByVal nXDest As Integer, _
        ByVal nYDest As Integer, _
        ByVal nWidth As Integer, _
        ByVal nHeight As Integer, _
        ByVal hdcSrc As IntPtr, _
        ByVal nXSrc As Integer, _
        ByVal nYSrc As Integer, _
        ByVal dwRop As System.Int32) As Boolean
        End Function

        Public Sub CaptureImage(ByVal Browser1 As Integer, ByVal Browser2 As Integer)
            SetCaptureImage.BitBlt(dc2, 0, 0, 85, 85, dc1, Browser1, Browser2, 13369376)
            g1.ReleaseHdc(dc1)
            g2.ReleaseHdc(dc2)
        End Sub

    End Class

    Code Execution

    Dim try1 As New SetCaptureImagePointers()
    try1.CaptureImage(AxWebBrowser1.Location.X, AxWebBrowser1.Location.Y)
    Myform2.thumbScr1.Image = try1.MyImage.GetThumbnailImage(50, 50, Nothing, Nothing)

  • helis

    Hey Justin, It works but there are some problems.

    The Code below works fine, in fact I do not even have to save the file in order to thumbnail it.  I can use the graphics object by pumping it into the Thumbnail routine directly.

    The problem is with Source Coordinates.  I’m having a problem identifying a way to use relative coordinates so that no matter where the form is on the screen I can grab the right location.  i.e. if the form is moved during the session or the Form property position is set to manual and, “the next time”, startup position has changed… what t I grab is then not in the right place.

    One other issue:  When using one WebBrowser control on a form the above comments are the only issue.  When using multiple WebBrowser controls on a form and trying to capture a location with the correct coordinates and the same form location I capture, nine of ten times, the wrong location.   Another words:

    1.Form is placed in the same location. 
    2.The physical capture coordinates are correct based on X&Y coordinates of the WebBrowser control
    3.capturing the correct screen portion will work occasionally.

    I may be misunderstanding the function call any ideas

    Public myCapture As New CaptureClass()

    Public Class CaptureClass

        Public MyImageClass As Image
        <System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
        Public Shared Function BitBlt(ByVal hdcDest As IntPtr, _
        ByVal nXDest As Integer, _
        ByVal nYDest As Integer, _
        ByVal nWidth As Integer, _
        ByVal nHeight As Integer, _
        ByVal hdcSrc As IntPtr, _
        ByVal nXSrc As Integer, _
        ByVal nYSrc As Integer, _
        ByVal dwRop As System.Int32) As Boolean

        End Function


    Dim myform1 As New Form1()
    Dim g1 As Graphics = Me.CreateGraphics()
    Dim MyImage = New Bitmap(85, 85, g1)
    Dim g2 As Graphics = Graphics.FromImage(MyImage)
    Dim dc1 As IntPtr = g1.GetHdc()
    Dim dc2 As IntPtr = g2.GetHdc()

    myCapture.BitBlt(dc2, 0, 0, 85, 85, dc1, myform1.AxWebBrowser1.Location.X + 24, myform1.AxWebBrowser1.Location.X + 16, 13369376)

    Me.PictureBox1.Image = MyImage.GetThumbnailImage(35, 35, Nothing, Nothing)      



  • color

    Hey Justin,   GOT IT!

    The DC was incorrect should have pointed to Form1.  Started looking at the handles and realized I was pointing to… nothing… well not really nothing, but pretty close to it!  Silly mistake.  

    You saying that the points were relative got me looking at what structure I was pointing to.  Thanks! GregC

    'Dim g1 As Graphics = Myform2.CreateGraphics()
    Dim g1 As Graphics = Me.CreateGraphics()

    Dim dc1 As IntPtr = g1.GetHdc()

  • rmethod

    I don't believe there is any managed way to do this.  You'll need to do work using GDI to get the hDC of the browser control, select a region to blt across to an offscreen surface and then thumbnail that.  Most of this is going to be done using PInvoke calls.

    In addition, if the quality of the thumbnails are important, then don't use GetThumbnailImage, and instead using the DrawImage method to rescale the image down.  It may take a small amount of extra time relative to GetThumbnailImage, but the results are MUCH better.

  • fblair

    If the screen is scrollable, How can we capture the full screen

  • tushar.k

    Go ahead and run down the following debug steps to see if you can find your problem.  If not, I'll actually take the opportunity to jump in and really figure out what is going wrong here:

    1.  I think the pixel coordinates are already relative to the device context you are using.  So you just need to do 24 and 16, not Loc.X+24, etc...  This might be the cause of some items not showing up correctly.

    2.  I remember having to work with Twips, a unit of measure that is display independent, however, I don't even see mention of Twips in the current documentation.  Probably a moot point.

    Hope this helps you solve your issue.  Other items to consider are client vs screen points.  There are some functions to tranlsate between client points and screen points through Windows Forms, but I don't think you'll need to use them.

  • Rodger Reed

    Glad to see you got this working on your end.  Time to wrap it up in a small code sample/reusable snippet I think.  I'll see about doing this over the course of the next day or two.  Some items to think about are the ever popular StretchBlt function which will probably automatically do the resizing/resampling for thumbnail sizes during the actual Blt operation.
  • Thumbnail an html page directly from IE programmatically?