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.
ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Capture
Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C#
and VB.Net</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<b>Enter WebSite Url:</b>
</td>
<td>
<asp:TextBox ID="txtUrl"
runat="server"
/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnCapture"
Text="Capture"
runat="server"
OnClick="btnCapture_click"
/>
</td>
</tr>
</table>
<br />
<asp:Image ID="imgScreenshot" runat="server" Visible="false" Height="800" Width="800" />
</form>
</body>
</html>
|
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.Navigate(url);
browser.Width
= 1024;
browser.Height
= 768;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webbrowse_DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
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:
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.Navigate(url)
browser.Width = 1024
browser.Height = 768
browser.DocumentCompleted += New
WebBrowserDocumentCompletedEventHandler(AddressOf
webbrowse_DocumentCompleted)
While browser.ReadyState <>
WebBrowserReadyState.Complete
System.Windows.Forms.Application.DoEvents()
End While
End Using)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
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
|
No comments:
Post a Comment