Tuesday, 16 September 2014

How to Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C# and VB.Net

Hi friends,in this article I will explain about How to Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C# and VB.Net
To capture the screenshot (snapshot) of a Website web page, I am making use of WebBrowser control of Windows Forms Application.
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.

<html xmlns="http://www.w3.org/1999/xhtml">
    <title>Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C#
        and VB.Net</title>
    <form id="form1" runat="server">
                <b>Enter WebSite Url:</b>
                <asp:TextBox ID="txtUrl" runat="server" />
                <asp:Button ID="btnCapture" Text="Capture" runat="server" OnClick="btnCapture_click" />
    <br />
    <asp:Image ID="imgScreenshot" runat="server" Visible="false" Height="800" Width="800" />

In C#:
using System.IO;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

public partial class CaptureWebsite : System.Web.UI.Page
    protected void btnCapture_click(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.Width = 1024;
                browser.Height = 768;
                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webbrowse_DocumentCompleted);
                while (browser.ReadyState != WebBrowserReadyState.Complete)

    private void webbrowse_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        WebBrowser webrowse = sender as WebBrowser;
        Bitmap bitmap = new Bitmap(webrowse.Width, webrowse.Height);
        webrowse.DrawToBitmap(bitmap, webrowse.Bounds);
        MemoryStream stream = new MemoryStream();
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] strbytes = stream.ToArray();
        imgScreenshot.Visible = true;
        imgScreenshot.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(strbytes);


Imports System.IO
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms

Partial Public Class CaptureWebsite
    Inherits System.Web.UI.Page
    Protected Sub btnCapture_click(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.Width = 1024
        browser.Height = 768
        browser.DocumentCompleted += New WebBrowserDocumentCompletedEventHandler(AddressOf webbrowse_DocumentCompleted)
        While browser.ReadyState <> WebBrowserReadyState.Complete
        End While
                   End Using)
    End Sub

    Private Sub webbrowse_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
        Dim webrowse As WebBrowser = TryCast(sender, WebBrowser)
        Dim bitmap As New Bitmap(webrowse.Width, webrowse.Height)
        webrowse.DrawToBitmap(bitmap, webrowse.Bounds)
        Dim stream As New MemoryStream()
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim strbytes As Byte() = stream.ToArray()
        imgScreenshot.Visible = True
        imgScreenshot.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(strbytes)
    End Sub

End Class

The output of the above code as shown in the below figure.

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.