WebBrowser:BrowsePage.Document.Body.ScrollRectangle.Width

时间:2012-03-06 18:52:28

标签: vb.net

我遇到以下代码问题。即使页面比这个页面大很多,BrowsePage.Document.Body.ScrollRectangle.Width返回230。因此,页面的大部分内容都被删除了。如果我用一个整数替换BrowsePage.Document.Body.ScrollRectangle.Width,例如2000年,整个图像被捕获。正在捕获的HTML文件(c:\ htmlTest.txt)确实包含一些JavaScript错误:

Public Class ImageFromHtml
    Private PageUrl As String
    Private ConvertedImage As Bitmap
    Private BrowsePage As New WebBrowser()
    Public documentText As String

    Private m_intHeight As Integer
    Public Property Height() As Integer
        Get
            Return m_intHeight
        End Get
        Set(ByVal value As Integer)
            m_intHeight = value
        End Set
    End Property

    Private m_intWidth As Integer
    Public Property Width() As Integer
        Get
            Return m_intWidth
        End Get
        Set(ByVal value As Integer)
            m_intWidth = value
        End Set
    End Property

    Public Function ConvertPage() As Bitmap
        'Me.PageUrl = PageUrl
        Dim thrCurrent As New Thread(New ThreadStart(AddressOf CreateImage))
        thrCurrent.SetApartmentState(ApartmentState.STA)
        thrCurrent.Start()
        thrCurrent.Join()
        'CreateImage()
        Return ConvertedImage
    End Function
    Private Sub CreateImage()

        BrowsePage.ScrollBarsEnabled = False
        'BrowsePage.Navigate(PageUrl)
        ' BrowsePage.ScriptErrorsSuppressed = True
        'BrowsePage.Navigate("about:blank")
        BrowsePage.DocumentText = documentText
        BrowsePage.ScrollBarsEnabled = True
        AddHandler BrowsePage.DocumentCompleted, AddressOf _
WebBrowser_DocumentCompleted
        While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        End While
        BrowsePage.Dispose()
    End Sub



     Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As  _
    WebBrowserDocumentCompletedEventArgs)
            'BrowsePage = DirectCast(sender, WebBrowser)
            BrowsePage = DirectCast(sender, WebBrowser)
            Height = BrowsePage.Document.Body.ScrollRectangle.Height
            Width = BrowsePage.Document.Body.ScrollRectangle.Width
            BrowsePage.ClientSize = New Size(BrowsePage.Document.Body.ScrollRectangle.Width, BrowsePage.Document.Body.ScrollRectangle.Height)
            BrowsePage.ScrollBarsEnabled = True
            BrowsePage.ScriptErrorsSuppressed = True
            'ConvertedImage = New Bitmap(Width, Height)
            ConvertedImage = New Bitmap(BrowsePage.Document.Body.ScrollRectangle.Width, BrowsePage.Document.Body.ScrollRectangle.Height)
            BrowsePage.BringToFront()
            BrowsePage.DrawToBitmap(ConvertedImage, BrowsePage.Bounds)
            'MsgBox(BrowsePage.Document.Body.ScrollRectangle.Height)
            'MsgBox(BrowsePage.Document.Body.ScrollRectangle.Width)
        End Sub



    End Class

Public Class Form1



     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                Dim objImageFromHtml As New ImageFromHtml()
                'objImageFromHtml.Height = 1000
                'objImageFromHtml.Width = 1000
                Dim SR = New StreamReader("c:\htmlTest.txt")
                objImageFromHtml.documentText = SR.ReadToEnd
                Dim Test As Bitmap = objImageFromHtml.ConvertPage()
                Test.Save("c:\Test.jpeg", _
                System.Drawing.Imaging.ImageFormat.Jpeg)
            Catch ex As Exception

            End Try
        End Sub
    End Class

1 个答案:

答案 0 :(得分:0)

潜在的一些问题:

  • 当您以这种方式使用DocumentComplete时,网页可能没有完成完整的布局和渲染,特别是如果有动态内容,这将不会影响控件的测量(如果JavaScript会导致更多内容被加载,显示等等。

  • .NET 4.0,DrawToBitmap不支持您调用的render方法。所以它不会像你想要的那样渲染IE的内容(事实上,它只是为我渲染一个大的白色块,事实上,我无法在.NET 3.5sp1或4.0中使用IE9渲染它)

  • DocumentCompleted Event中的高度和宽度设置对我来说不清楚......你想在那里放大什么?我希望BrowsePage成为你想要调整大小的东西吗?