In this article I will explainhow to use the Windows Forms WebBrowser control in ASP.Net Website using C# and VB.Net.
The WebBrowser control will be used to capture the screenshot of a Website web page.
 
Referencing the WebBrowser control in ASP.Net
Since the WebBrowser is a Windows Forms controls, in order to use it in ASP.Net Web Projects, we will have to add reference to the following libraries.
Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C# and VB.Net
 
 
HTML Markup
The HTML Markup consist of a TextBox, a Button and an Image control.
<asp:TextBox ID="txtUrl" runat="server" Text = "" />
<asp:Button Text="Capture" runat="server" OnClick="Capture" />
<br />
<asp:Image ID="imgScreenShot" runat="server" Height="300" Width="400" Visible = "false" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
 
VB.Net
Imports System.IO
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
 
 
Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C# and VB.Net
When the Button is clicked the following event handler is raised, here a new Thread is initiated and the WebBrowser control is initialized within the Thread. For the WebBrowser I have attached a DocumentCompleted event handler which will be raised when the Web Page is loaded inside the browser.
Note: Since ASP.Net applications work in Multi-Threaded Apartments (MTA) we need to start a new Thread for the WebBrowser control and execute the Thread in Single-Threaded Apartments (STA).
Inside the DocumentCompleted event handler, the Web Page loaded inside the WebBrowser control is drawn to a Bitmap which is then converted to a Byte Array and then to Base64 string so that it can be displayed in the Image control.
To know more about Base64 string, please refer Display byte Array as Image without using Generic Handler in ASP.Net

C#
protected void Capture(object sender, EventArgs e)
{
    string url = txtUrl.Text.Trim();
    Thread thread = new Thread(delegate()
    {
        using (WebBrowser browser = new WebBrowser())
        {
            browser.ScrollBarsEnabled = false;
            browser.AllowNavigation = true;
            browser.Navigate(url);
            browser.Width = 1024;
            browser.Height = 768;
            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
            while (browser.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents();
            }
        }
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}
 
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;
    using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
    {
        browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            byte[] bytes = stream.ToArray();
            imgScreenShot.Visible = true;
            imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
        }
    }
}
 
VB.Net
Protected Sub Capture(sender As Object, e As EventArgs)
    Dim url As String = txtUrl.Text.Trim()
    Dim thread As New Thread(Sub()
                                 Using browser As New WebBrowser()
                                     browser.ScrollBarsEnabled = False
                                     browser.AllowNavigation = True
                                     browser.Navigate(url)
                                     browser.Width = 1024
                                     browser.Height = 768
                                     AddHandler browser.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompleted)
                                     While browser.ReadyState <> WebBrowserReadyState.Complete
                                         System.Windows.Forms.Application.DoEvents()
                                     End While
                                 End Using
                             End Sub)
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start()
    thread.Join()
End Sub
 
Private Sub DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    Dim browser As WebBrowser = TryCast(sender, WebBrowser)
    Using bitmap As New Bitmap(browser.Width, browser.Height)
        browser.DrawToBitmap(bitmap, New Rectangle(0, 0, browser.Width, browser.Height))
        Using stream As New MemoryStream()
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
            Dim bytes As Byte() = stream.ToArray()
            imgScreenShot.Visible = True
            imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes)
        End Using
    End Using
End Sub
 
Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C# and VB.Net
 
Downloads