Introduction:
Here I will explain how to capture or take screenshot of web page in asp.net or how to get snapshot of website web page in asp.net using C# and VB.NET. To capture screenshot or snapshot of web page inasp.net we need to use WebBrowser control in windows forms application.
Description:
In previous articles I explained Show Gridview row details in tooltip, Show tooltip for gridview header columns, jQuery change tooltip style in asp.net, Add tooltip to dropdownlist items in asp.net, Create simple tooltip with jQuery UI plugin, Asp.net Interview questions, Highlight Gridview records based on search and many articles relating to Gridview, SQL, jQuery,asp.net, C#,VB.NET. Now I will explain how to capture or take screenshot of web page in asp.net
To capture web page screenshot first create new website à Right click on website à Select Add Reference à Select System.Windows.Forms and click OK
Once we add System.Windows.Forms reference to our application then we need to write the code like as shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>take snapshot(screenshot) of webpage in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><b>Enter Web Url:</b></td>
<td><asp:TextBox ID="txtweburl" runat="server" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnscreenshot" Text="Take Screenshot" runat="server" OnClick="btnscreenshot_click" />
</td>
</tr>
</table>
<br />
<asp:Image ID="imgscreenshot" runat="server" Visible = "false" Height="500" Width="500" />
</form>
</body>
</html>
|
Now in code behind add the following namespaces
C# Code
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
|
After that add below code in code behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnscreenshot_click(object sender, EventArgs e)
{
Thread thread = new Thread(GenerateThumbnail);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void GenerateThumbnail()
{
WebBrowser webrowse = new WebBrowser();
webrowse.ScrollBarsEnabled = false;
webrowse.AllowNavigation = true;
webrowse.Navigate(txtweburl.Text.Trim());
webrowse.Width = 1024;
webrowse.Height = 768;
webrowse.DocumentCompleted += webbrowse_DocumentCompleted;
while (webrowse.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
private void webbrowse_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgse)
{
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);
}
|
VB.NET Code
Imports System.Drawing
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Partial Class vbcode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnscreenshot_click(ByVal sender As Object, ByVal e As EventArgs)
Dim thread As New Thread(AddressOf GenerateThumbnail)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
End Sub
Private Sub GenerateThumbnail()
Dim webrowse As New WebBrowser()
webrowse.ScrollBarsEnabled = False
webrowse.AllowNavigation = True
webrowse.Navigate(txtweburl.Text.Trim())
webrowse.Width = 1024
webrowse.Height = 768
AddHandler webrowse.DocumentCompleted, AddressOf webbrowse_DocumentCompleted
While webrowse.ReadyState <> WebBrowserReadyState.Complete
System.Windows.Forms.Application.DoEvents()
End While
End Sub
Private Sub webbrowse_DocumentCompleted(ByVal sender As Object, ByVal e AsWebBrowserDocumentCompletedEventArgs)
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
|
0 comments:
Post a Comment