themhz
themhz

Reputation: 8424

Get ASP.NET VB.NET Website Thumbnail Screenshot

Hello I managed to use the class ClassWSThumb (you can find it here http://www.codeproject.com/KB/aspnet/Website_URL_Screenshot.aspx) in order to get screenshoots from webpages. Here is the class file:

Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading
Imports System.IO

Namespace GetWebSiteThumb
    Public Class ClassWSThumb
        Public Shared Function GetWebSiteThumbnail(ByVal Url As String, ByVal BrowserWidth As Integer, ByVal BrowserHeight As Integer, ByVal ThumbnailWidth As Integer, ByVal ThumbnailHeight As Integer) As Bitmap
            Return New WSThumb(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight).GetWSThumb()
        End Function

        Private Class WSThumb
            Public Sub New(ByVal Url As String, ByVal BW As Integer, ByVal BH As Integer, ByVal TW As Integer, ByVal TH As Integer)
                __Url = Url
                __BrowserWidth = BW
                __BrowserHeight = BH
                __ThumbnailWidth = TW
                __ThumbnailHeight = TH
            End Sub

            Private __Bitmap As Bitmap = Nothing
            Private __Url As String = Nothing
            Private __ThumbnailWidth As Integer
            Private __ThumbnailHeight As Integer
            Private __BrowserWidth As Integer
            Private __BrowserHeight As Integer

            Public Property Url() As String
                Get
                    Return __Url
                End Get
                Set(ByVal value As String)
                    __Url = value
                End Set
            End Property

            Public ReadOnly Property ThumbnailImage() As Bitmap
                Get
                    Return __Bitmap
                End Get
            End Property

            Public Property ThumbnailWidth() As Integer
                Get
                    Return __ThumbnailWidth
                End Get
                Set(ByVal value As Integer)
                    __ThumbnailWidth = value
                End Set
            End Property

            Public Property ThumbnailHeight() As Integer
                Get
                    Return __ThumbnailHeight
                End Get
                Set(ByVal value As Integer)
                    __ThumbnailHeight = value
                End Set
            End Property

            Public Property BrowserWidth() As Integer
                Get
                    Return __BrowserWidth
                End Get
                Set(ByVal value As Integer)
                    __BrowserWidth = value
                End Set
            End Property

            Public Property BrowserHeight() As Integer
                Get
                    Return __BrowserHeight
                End Get
                Set(ByVal value As Integer)
                    __BrowserHeight = value
                End Set
            End Property

            Public Function GetWSThumb() As Bitmap
                Dim __threadStart As New ThreadStart(AddressOf _GenerateWSThumb)
                Dim __thread As New Thread(__threadStart)

                __thread.SetApartmentState(ApartmentState.STA)
                __thread.Start()
                __thread.Join()
                Return __Bitmap
            End Function

            Private Sub _GenerateWSThumb()
                Dim __WebBrowser As New WebBrowser()
                __WebBrowser.ScrollBarsEnabled = False
                __WebBrowser.Navigate(__Url)
                AddHandler __WebBrowser.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf WebBrowser_DocumentCompleted)
                While __WebBrowser.ReadyState <> WebBrowserReadyState.Complete
                    Application.DoEvents()
                End While
                __WebBrowser.Dispose()
            End Sub

            Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
                Dim __WebBrowser As WebBrowser = DirectCast(sender, WebBrowser)
                __WebBrowser.ClientSize = New Size(Me.__BrowserWidth, Me.__BrowserHeight)
                __WebBrowser.ScrollBarsEnabled = False
                __Bitmap = New Bitmap(__WebBrowser.Bounds.Width, __WebBrowser.Bounds.Height)
                __WebBrowser.BringToFront()
                __WebBrowser.DrawToBitmap(__Bitmap, __WebBrowser.Bounds)

                If __ThumbnailHeight <> 0 AndAlso __ThumbnailWidth <> 0 Then
                    __Bitmap = DirectCast(__Bitmap.GetThumbnailImage(__ThumbnailWidth, __ThumbnailHeight, Nothing, IntPtr.Zero), Bitmap)
                End If
            End Sub
        End Class
    End Class
End Namespace

and here is the implementation

    Protected Sub btnExportToIMG_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToIMG.Click
        'This Code Exports to Image 
        StartDuration()        
        Dim url As String = "http://somepage"

        '//example as a Class Method
        Dim bmp As Bitmap = ClassWSThumb.GetWebSiteThumbnail(url, Int32.Parse(800), Int32.Parse(600), Int32.Parse(800), Int32.Parse(600))
        bmp.Save(Server.MapPath("~") + "/exportedpdf/thumbnail.bmp")
        Image1.ImageUrl = "~/thumbnail.bmp"
        Image1.Width = 800
        Image1.Height = 600
        StopDuration()


    End Sub
End Class

Everything seems to be working perfectly, but i bumped into a situation were i dont know how to handle. If the target website has some ajax information that needs to be loaded after the ready state, i get a screenshoot of the page with no data, because the data was not loaded yet.

any suggestions?

Upvotes: 2

Views: 4848

Answers (2)

Kun
Kun

Reputation: 11

excellent code. I had a problem when printing the page, the page could not load all the javascript, I made a modification that I present below. and it worked perfectly, I hope you find it useful.

       Private Sub _GenerateWSThumb()
        Dim __WebBrowser As New WebBrowser()
        __WebBrowser.ScrollBarsEnabled = False
        __WebBrowser.ObjectForScripting = True
        __WebBrowser.Navigate(__Url)

        While __WebBrowser.ReadyState <> WebBrowserReadyState.Complete
            Windows.Forms.Application.DoEvents()
        End While
       ' start my modification 
        AddHandler __WebBrowser.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf WebBrowser_DocumentCompleted)

        Dim e As WebBrowserDocumentCompletedEventArgs
        WebBrowser_DocumentCompleted(__WebBrowser, e)
       ' end my modification
        __WebBrowser.Dispose()
    End Sub

'Ajax and Javascript fully loaded

Upvotes: 1

NoAlias
NoAlias

Reputation: 9193

Try adding the following at the top of the WebBrowser_DocumentCompleted Sub Routine...

'Give the Browser 15 seconds to load Ajax data.
System.Threading.Thread.Sleep(15000)

Upvotes: 1

Related Questions