Web Browser control crashes
<p>I have a Web Browser object that I to take a screenshot.  The code can be found here: <a href="http://www.vbknowledgebase.com/?Id=65&amp;Desc=Convert-Web-Page-or-Html-to-Image" rel="nofollow">http://www.vbknowledgebase.com/?Id=65&amp;Desc=Convert-Web-Page-or-Html-to-Image</a> (I have modified it a little).</p>

<p>It appears that if I deploy the application locally and then attempt to capture a screenshot of a local page e.g. <a href="http://localhost/localpage" rel="nofollow">http://localhost/localpage</a>, 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. <a href="http://www.google.co.uk" rel="nofollow">http://www.google.co.uk</a>, it works perfectly.</p>

<p>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.</p>

<p>Here is the full code:</p>

<p>'ImageFromHTML.vb</p>

<pre><code>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 &lt;&gt; 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
</code></pre>

<p>'Default.aspx.vb</p>

<pre><code>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
</code></pre>

<p>If I change the PageURL variable to an external website e.g. <a href="http://www.google.co.uk" rel="nofollow">http://www.google.co.uk</a>, it works.  It does not work as it stands above.</p>
