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 in asp.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, 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);
}
|
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 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
|
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
12 comments :
Sir can I store values of multiple rows of textboxes in MsAccess Database using single "Save " button with different id.
How can I do it...Please reply as soon possible.......Thank You....
can we capture screen shot without using thread?
How can we capture screenshot with Forms Authentication?
Thank You.........
very helpful information .... thanks!!
Can we use this in MVC 4
I tried to capture screen shot, Its working in my local machine but it doesn't work in live site, screenshot shows as "Navigation to the Web Page was cancelled"
It's is not working in web server, It's only work on local system. When i upload the local system it is not working.
showing this error.
Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 0
pls help me
Hi...I am getting error while executing this code...could you please help:
Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct
Hi It doesn't produce the content of given URL instead it just created a WHITE image having no content.
Please revert, if I am missing something.
Can we take full page screen shot or only the visible portion?
Note: Only a member of this blog may post a comment.