CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: HTML Thunbnail

  1. #1
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Question HTML Thunbnail

    What I am looking for should be fairly straight forward!!!

    I need to show a thumbnail of a web page on a VB 2008 form. I have found a number of code snippets that mention something called an IViewObject which can apparently do what I need but the only examples are in C.

    I am relatively new to VB2008 and I haven't written anything in C for about 15 years, so any help on this issue would be greatly appreciated.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: HTML Thunbnail

    Is it only one web page you need the thumbnail for Killa ¿

  3. #3
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: HTML Thunbnail

    I only need one page at a time. I have a little mailshot program that sends emails as HTML from a HTML template file which is the file that I need to thumbnail. I am hoping this will reduce the chances of the user selecting the wrong template file.

    I have been trying to access IViewObject with no success.

    I have got the WebBrowser DrawToBitmap function working to some degree but this only seems to work on certain pages. A lot of the time this function is returning a white/blank image. (something to do with framing?)

    Code:
    Public Class Form1
        Dim PageLoading As Boolean
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Button1.Enabled = False
            PageLoading = True
            Me.WebBrowser1.Navigate(TextBox1.Text)
            Do
                Application.DoEvents()
            Loop While PageLoading
            WebBrowser1.Height = WebBrowser1.Document.Body.ScrollRectangle.Height
            WebBrowser1.Width = WebBrowser1.Document.Body.ScrollRectangle.Width
    
            Dim bm As Bitmap
            bm = New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
            WebBrowser1.DrawToBitmap(bm, WebBrowser1.Bounds)
            bm.Save("C:\test1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            PictureBox1.Image = System.Drawing.Image.FromFile("C:\test1.jpg")
            bm.Dispose()
    
            Button1.Enabled = True
    
        End Sub
    
    
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            PageLoading = False
        End Sub
    End Class
    I think the DrawToBitmap function is not fully supported on WebBrowser but this is the only one that I've managed to get working to any degree so far.

    Also, there are third party controls that do this but I am trying to avoid these for the moment.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: HTML Thunbnail

    Hello again Killa!

    I have managed to find something for you - this is not my code
    This Thread has a very nice example :
    http://www.vbforums.com/showthread.p...page+thumbnail

    It says 2008, but I've done it in 2005 as well

    Start a new Windows Forms Project.
    Add a Webbrowser component ( from the Toolbox ) to your form.
    Add a new class and add the following :
    Code:
    Imports System
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Windows.Forms
    Imports System.Diagnostics
    
    
    Public Class GetImage
        Private S_Height As Integer
        Private S_Width As Integer
        Private F_Height As Integer
        Private F_Width As Integer
        Private MyURL As String
    
        Property ScreenHeight() As Integer
            Get
                Return S_Height
            End Get
            Set(ByVal value As Integer)
                S_Height = value
            End Set
        End Property
    
        Property ScreenWidth() As Integer
            Get
                Return S_Width
            End Get
            Set(ByVal value As Integer)
                S_Width = value
            End Set
        End Property
    
        Property ImageHeight() As Integer
            Get
                Return F_Height
            End Get
            Set(ByVal value As Integer)
                F_Height = value
            End Set
        End Property
    
        Property ImageWidth() As Integer
            Get
                Return F_Width
            End Get
            Set(ByVal value As Integer)
                F_Width = value
            End Set
        End Property
    
        Property WebSite() As String
            Get
                Return MyURL
            End Get
            Set(ByVal value As String)
                MyURL = value
            End Set
        End Property
    
        Sub New(ByVal WebSite As String, ByVal ScreenWidth As Integer, ByVal ScreenHeight As Integer, ByVal ImageWidth As Integer, ByVal ImageHeight As Integer)
            Me.WebSite = WebSite
            Me.ScreenWidth = ScreenWidth
            Me.ScreenHeight = ScreenHeight
            Me.ImageHeight = ImageHeight
            Me.ImageWidth = ImageWidth
        End Sub
    
        Function GetBitmap() As Bitmap
            Dim Shot As New WebPageBitmap(Me.WebSite, Me.ScreenWidth, Me.ScreenHeight)
            Shot.GetIt()
            Dim Pic As Bitmap = Shot.DrawBitmap(Me.ImageHeight, Me.ImageWidth)
            Return Pic
        End Function
    End Class
    
    Class WebPageBitmap
        Dim MyBrowser As WebBrowser
        Dim URL As String
        Dim Height As Integer
        Dim Width As Integer
    
        Sub New(ByVal url As String, ByVal width As Integer, ByVal height As Integer)
            Me.Height = Height
            Me.Width = width
            Me.URL = url
            MyBrowser = New WebBrowser
            MyBrowser.ScrollBarsEnabled = False
            MyBrowser.Size = New Size(Me.Width, Me.Height)
        End Sub
    
        Sub GetIt()
            MyBrowser.Navigate(Me.URL)
            While MyBrowser.ReadyState <> WebBrowserReadyState.Complete
                Application.DoEvents()
            End While
        End Sub
    
        Function DrawBitmap(ByVal theight As Integer, ByVal twidth As Integer) As Bitmap
            Dim myBitmap As New Bitmap(Width, Height)
            Dim DrawRect As New Rectangle(0, 0, Width, Height)
            MyBrowser.DrawToBitmap(myBitmap, DrawRect)
            Dim imgOutput As System.Drawing.Image = myBitmap
            Dim oThumbNail As System.Drawing.Image = New Bitmap(twidth, theight, imgOutput.PixelFormat)
            Dim g As Graphics = Graphics.FromImage(oThumbNail)
            g.CompositingQuality = Drawing2D.CompositingQuality.HighSpeed
            g.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
            Dim oRectangle As Rectangle
            oRectangle = New Rectangle(0, 0, twidth, theight)
            g.DrawImage(imgOutput, oRectangle)
            Try
                Return oThumbNail
            Catch ex As Exception
            Finally
                imgOutput.Dispose()
                imgOutput = Nothing
                MyBrowser.Dispose()
                MyBrowser = Nothing
            End Try
        End Function
    
    End Class
    Then, inside your event ( wherever you need it on the Form, ie. Button click etc. ) type this :
    Code:
    Public Class Form1
    
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim g As New GetImage("http://www.ncc-cla.com", 200, 300, 200, 300) 'Whatever size you require 
            g.GetBitmap.Save("C:\Temp.jpg")
    
        End Sub
    End Class
    Voila! There is your Thumbnail

    I hope this helps

  5. #5
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Thumbs up Re: HTML Thunbnail

    Hannes, this is definitely working better than my attempt.

    Strange thing is that this still doesn't work with Google (http://www.google.co.uk). Not a problem for what I need this for but still strange none the less.

    Many thanks for your help on this.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: HTML Thunbnail

    That's good news!
    Good work!

    I'll see if I can find a reason as to why this doesn't work with google.co.uk ( and perhaps others )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured