Ref:Capture Screenshot (Snapshot) Image of Website (Web Page) in ASP.Net using C# and VB.Net
Here i have added the iFrame in my page:
<form id="form1" runat="server">
<asp:TextBox ID="txtUrl" runat="server" Text="" />
<iframe id="iframeDemo" runat="server" src="http://aspsnippets.com/" height="500px"
width="500px"></iframe>
<asp:Button Text="Capture" runat="server" OnClick="Capture" />
<br />
<asp:Image ID="imgScreenShot" runat="server" Height="500" Width="700" Visible="false" />
</form>
Src of IFram is http://aspsnippets.com/. So when i click on the button then it will take the Screen shot of the Src.
C#:
protected void Capture(object sender, EventArgs e)
{
//string url = txtUrl.Text.Trim();
string url = this.iframeDemo.Attributes["src"].ToString();
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(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
{
browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byte[] bytes = stream.ToArray();
imgScreenShot.Visible = true;
imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
}
}
}
In this i have commented TextBox value and use the Src value for taking Screen shot
Thank You.