获取ASP.NET VB.NET网址缩略截图 [英] Get ASP.NET VB.NET Website Thumbnail Screenshot

查看:148
本文介绍了获取ASP.NET VB.NET网址缩略截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我设法使用类ClassWSThumb(你可以在这里找到它 HTTP ://www.$c$cproject.com/KB/aspnet/Website_URL_Screenshot.aspx )为了从网页获得screenshoots。
下面是类文件:

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

和这里是实施

    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的信息,我得到无数据页面的screenshoot,因为数据还没有加载。

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.

有什么建议?

推荐答案

尝试添加在WebBrowser_DocumentCompleted顶部以下子程序...

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)

这篇关于获取ASP.NET VB.NET网址缩略截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆