Thursday, 17 April 2014

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

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:


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 ObjectByVal e As EventArgs)
End Sub
Protected Sub btnscreenshot_click(ByVal sender As ObjectByVal 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 ObjectByVal 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

Related Posts:

  • கணனியின் வேகத்தை அதிகரிக்க ஒரு மென்பொருள் உங்கள் கணினி மெதுவாக இயங்குகிறதா உங்கள் கணினி ஸ்பீட் ஆக வேண்டுமா சில கணினிகள் மெதுவாக இயங்கும்.சில கணினிகள் ON ஆவதற்கே பல மணி நேரம் எடுத்து கொள்ளும்.அதனாலேயே கணினி வைத்திருக்கும் சிலர் அந்த கணினியை தொடுவதே இல்லை.ஆனால் இனி… Read More
  • Hard Disk இல் ஏற்படும் பிரச்சனைகளை சரிசெய்ய அதிக நாட்களாக பயன்படுத்தப்படும் வன்தட்டுகளில் பலவிதமான கோளாருச் செய்திகள் காணப்படும். விண்டோஸ் நிறுவப்பட்டுள்ள கணினியில் இதுபோன்ற கோளாருச் செய்திகள் அதிகமாக காணப்படும். வன்தட்டில் மென்பொருள்களை நிறுவி பயன்படுத்துவோம் … Read More
  • அதிகம் கையடக்க தொலைபேசியில் பேசுவோர் கவனத்திற்கு ! இன்றைய அறிவியல் உலகம் தகவல் தொடர்பு தொழில் நுட்பங்களின் மூலம் மிக உன்னதமான பிணைப்பை உலக மக்களிடையே எளிமையாக்கிவிட்டது. கண்ணிமைக்கும் நேரத்தில் கடல் கடந்து பறக்கும் குரல் ஒலிகளின் ஒப்புயவர்வற்ற செயல்பாடுகளுக்கு செல்பேச… Read More
  • கணனியில் சில சுவாரஸ்யங்கள்! மேட்ரிக்ஸ் எப்படி இருக்கும் என பார்த்துருப்பீர்கள்.இதையே கனினியில் கொண்டு வரலாம்.அதற்கான சுலபமான வழிகள் இதோ1.Notepad ஐ அதாவது புதிய Text document திறந்து கொள்ளுங்கள்.2.கீழே உள்ள கோட்களை அப்படியே காப்பி செய்து கொள்ளுங்கள்.… Read More
  • நீங்கள் வீடியோ எடிட்டிங் செய்பவரா இதோ உங்களுக்கு ஒரு அசத்தலான மென்பொருள் ! வீடியோ எடிட்டிங் செய்யும் நண்பர்களுக்கு ஒரு மகிழ்ச்சியான செய்தி  அதாவது நம் வீடியோவை எடிட் செய்து ஹாலிவுட் தரத்தில் காட்ட ஒரு இலவச மென்பொருள் வந்துள்ளது இதைப்பற்றித்தான் இந்தப்பதிவு. வீடியோ எடிட்டிங் செய்ய பல மென்… Read More

0 comments:

Post a Comment