Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Capture Screenshot (Snapshot) of Web Page (Website) from url in c#

Apr 15, 2014
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:


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

Download Sample Code Attached


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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

12 comments :

Anonymous said...

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

Unknown said...

can we capture screen shot without using thread?

Unknown said...

How can we capture screenshot with Forms Authentication?

Bhavesh Bhuva said...

Thank You.........

Unknown said...

very helpful information .... thanks!!

Anonymous said...

Can we use this in MVC 4

Anonymous said...

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"

Unknown said...

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

Unknown said...

pls help me

Anonymous said...

Hi...I am getting error while executing this code...could you please help:
Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct

Pankaj Bahuguna said...

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.

Sidharth Mahapatra said...

Can we take full page screen shot or only the visible portion?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.