获取ASP.NET VB.NET网站缩略图截图

时间:2011-12-16 18:15:34

标签: asp.net vb.net

您好我设法使用ClassWSThumb类(您可以在http://www.codeproject.com/KB/aspnet/Website_URL_Screenshot.aspx找到它),以便从网页获取屏幕截图。 这是类文件:

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

这是实现

    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

一切似乎都很完美,但我碰到了一个情况,我不知道如何处理。 如果目标网站有一些需要在就绪状态之后加载的ajax信息,我会得到没有数据的页面的屏幕截图,因为数据尚未加载。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

尝试在WebBrowser_DocumentCompleted Sub Routine的顶部添加以下内容...

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

答案 1 :(得分:1)

优秀的代码。 我在打印页面时遇到问题,页面无法加载所有的javascript,我做了一个修改,我在下面提到。它完美无缺,我希望你觉得它很有用。

       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和Javascript已满载