Web浏览器控件崩溃 [英] Web Browser control crashes

查看:208
本文介绍了Web浏览器控件崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web浏览器对象,我采取的截图。在code可以在这里找到:的 http://www.vbknowledgebase.com/?Id=65&Desc=Convert-Web-Page-or-Html-to-Image (我已经修改了一点)。

I have a Web Browser object that I to take a screenshot. The code can be found here: http://www.vbknowledgebase.com/?Id=65&Desc=Convert-Web-Page-or-Html-to-Image (I have modified it a little).

看来,如果我在本地部署的应用程序,然后试图捕捉当地页例如截图的http://本地主机/ localpage ,程序挂起,并不会产生屏幕截图。但是,如果我在本地部署应用程序,并尝试例如产生外部网站的截屏 http://www.google.co.uk ,它完美的作品。

It appears that if I deploy the application locally and then attempt to capture a screenshot of a local page e.g. http://localhost/localpage, the program hangs and does not generate a screenshot. However, if I deploy the application locally and attempt to generate a screenshot of an external website e.g. http://www.google.co.uk, it works perfectly.

起初,我虽然是与STA(单线程公寓)的问题,所以我张贴的问题有关线程,但现在我不那么肯定。是否有与本地请求页面的Web浏览器对象的任何问题?我已经看过在MSDN和这里,但找不到任何定论。

At first I though it was a problem with the STA (single thread apartment), so I posted a question about threading but now I am not so sure. Are there any issues with the Web Browser object requesting local pages? I have looked on MSDN and on here but cannot find anything conclusive.

下面是完整的code:

Here is the full code:

ImageFromHTML.vb

'ImageFromHTML.vb

Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Drawing
Imports System.Windows.Forms

Public Class ImageFromHtml
    Private PageUrl As String = "http://localhost/Default2.aspx"
    Private ConvertedImage As Bitmap

    Private m_intHeight As Integer = 1000
    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 = 100
    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()
        ConvertedImage.Save("c:\Test.bmp", _
        System.Drawing.Imaging.ImageFormat.Bmp)
        Return ConvertedImage
    End Function
    Public Sub CreateImage()

        Dim BrowsePage As New WebBrowser()
        BrowsePage.ScrollBarsEnabled = False
        BrowsePage.Navigate(PageUrl)
        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)
        Dim BrowsePage As WebBrowser = DirectCast(sender, WebBrowser)
        BrowsePage.ClientSize = New Size(Width, Height)
        BrowsePage.ScrollBarsEnabled = False
        ConvertedImage = New Bitmap(Width, Height)
        BrowsePage.BringToFront()
        BrowsePage.DrawToBitmap(ConvertedImage, BrowsePage.Bounds)

    End Sub

    End Class

Default.aspx.vb

'Default.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim obj As New ImageFromHtml()
        obj.ConvertPage()
        obj = Nothing
    End Sub

如果我的PAGEURL变量更改为外部网站如 http://www.google.co.uk ,它的工作原理。按照上面代表它不能正常工作。

If I change the PageURL variable to an external website e.g. http://www.google.co.uk, it works. It does not work as it stands above.

推荐答案

Web控件这里没有崩溃,只是扔你使用的try-catch模式不能捕捉到的错误消息。

web control here is not crash, just throw error messages that you not capture using try-catch schema.

此功能使用 web浏览器类阅读页面并使其位图。

This functions use the WebBrowser class to read the page and make it bitmap.

所以以任何理由,也许是错误的端口,没有找到服务器,服务器返回什么等等,你会得到一个错误,你没有处理此错误抛出。扔是发生在导航,这里是从MSDN的http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigated.aspx

so for any reason, maybe the wrong port, not found the server, server return nothing etc, you get an error and you did not handle this error throw. The throw is happens on navigate, and here is an example from MSDN http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigated.aspx

' Navigates to the given URL if it is valid.
Private Sub Navigate(ByVal address As String)

    If String.IsNullOrEmpty(address) Then Return
    If address.Equals("about:blank") Then Return
    If Not address.StartsWith("http://") And _
        Not address.StartsWith("https://") Then
        address = "http://" & address
    End If

    Try
        webBrowser1.Navigate(New Uri(address))
    Catch ex As System.UriFormatException
        Return
    End Try

End Sub

但你可以使用之前的尝试捕捉。

but you can use the try catch before.

专注于你的code,这部分

Focus on this part of your code

While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
    Application.DoEvents()
End While

这是不是看起来合乎逻辑的正确的,因为如果该网页是不是从错误code竞争,或从未加载它,readyState的是永别的东西,那么你就堆在这里和线程永远不会结束加入休息!更改此code - 我不知道该怎么告诉你这里,因为我没有这个code调试它,但你可以很容易在这里举行一个破发点,看看code使环路如初。

This is not looks logical correct because if the web page is not Compete from an error code, or never load it and the ReadyState is something else for ever, then you stack here and the thread never ends to join the rest! Change this code - I do not know what to tell you here because I do not have this code to debug it, but you can easy place a break point here and see what code make it loop for ever.

看这个code

ConvertedImage.Save("c:\Test.bmp", _
System.Drawing.Imaging.ImageFormat.Bmp)

首先是越野车,和安全性错误code,反正池可能没有权限写入到C盘的根目录。

First of all is buggy, and security error code, anyway, the pool probably did not have permissions to write to the root of C drive.

这篇关于Web浏览器控件崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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