Export Web Page to Image In ASP.NET

ASP.NET is an open-source, server-side web application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic websites, applications and services. Wikipedia

In this post, I will show how to export a Webpage as an image in asp.net.

  1. Start Visual Studio 2005.
  2. Create a Web site.
  3. Add Reference of System.Windows.Forms
  4. Add AspCompat="true" in page attribute
  5. Verify that the following namespaces are included in the ExportWebPageToImage.c
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

Here is complete source code

<%@ Page Language="C#" AspCompat="true" AutoEventWireup="true" CodeFile="ExportWebPageToImage.aspx.cs"
   Inherits="ExportWebPageToImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
   <style type="text/css">
       .style1
       {
           width: 100%;
       }
   </style>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:TextBox ID="txtUrl" runat="server" Width="208px"></asp:TextBox><br />
       <asp:Button ID="btnConvert" runat="server" Text="Convert Page To Image" OnClick="btnConvert_Click" />
   </div>
   </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

public partial class ExportWebPageToImage : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {


   }

   public System.Drawing.Bitmap CaptureWebPage(string URL)
   {
       // create a hidden web browser, which will navigate to the page
       System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
       // we don't want scrollbars on our image
       web.ScrollBarsEnabled = false;
       // don't let any errors shine through
       web.ScriptErrorsSuppressed = true;
       // let's load up that page!
       web.Navigate(URL);

       // wait until the page is fully loaded
       while (web.ReadyState != WebBrowserReadyState.Complete)
           System.Windows.Forms.Application.DoEvents();
       System.Threading.Thread.Sleep(1500); // allow time for page scripts to update
       // the appearance of the page

       // set the size of our web browser to be the same size as the page
       int width = web.Document.Body.ScrollRectangle.Width;
       int height = web.Document.Body.ScrollRectangle.Height;
       web.Width = width;
       web.Height = height;
       // a bitmap that we will draw to
       System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
       // draw the web browser to the bitmap
       web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));

       return bmp; // return the bitmap for processing
   }
   protected void btnConvert_Click(object sender, EventArgs e)
   {
       Bitmap bitmap = new Bitmap(CaptureWebPage(txtUrl.Text));
       Response.ContentType = "image/jpeg";
       bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
       bitmap.Dispose();
       bitmap.Dispose();
       Response.End();
   }
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post