March 16, 2015 07:42 by
Peter
In this short article, I will tell you about How to Capture Screenshot Image of Website in ASP.NET 5. To catch the screenshot of page, I am making utilization of WebBrowser control of Windows Forms Application. Since the WebBrowser is a Windows Forms controls, so as to utilize it as a part of ASP.NET Web Projects, we will need to add reference to the accompanying libraries.
And here is the code that I used:
ASP.NET
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> How to Capture Screenshot in ASP.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>
Code for 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);
}
}
Code for 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
HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.