ASP. NET Implementation Method of Generating Web Page Thumbnail Based on URL

  • 2021-07-09 07:49:11
  • OfStack

In this paper, an example of ASP. NET based on URL to generate web page thumbnail method. Share it for your reference, as follows:

In the work, we need to use the function of generating webpage thumbnails according to URL, and make preparations in advance.

Find a source code on the Internet, but there is an error: the current thread is not in a single-threaded unit, so it is impossible to instantiate ActiveX control "8856f961-340a-11d0-a9", which runs well after solution, and is recorded here for standby!

Start page: Default. aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CaptureToImage._Default" %>
<!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 id="Head1" runat="server">
 <title>Snap</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <input type="button" onclick="window.open('Snap.aspx?url=www.njude.com.cn')" value=" Generate page thumbnails "/>
 </div>
 </form>
</body>
</html>

Call page: Snap. aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Snap.aspx.cs" Inherits="CaptureToImage.Snap" AspCompat="true" %>
<!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> Untitled page </title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 </div>
 </form>
</body>
</html>

PS: The red font part is the code added to solve the error, forcing the program to run in a single-threaded environment!

Calling page: Snap. aspx. cs


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
namespace CaptureToImage
{
 public partial class Snap : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   string url = string.Empty;
   url = Request.QueryString[0];
   try
   {
    GetImage thumb = new GetImage(url, 1024, 768, 800, 600);
    System.Drawing.Bitmap x = thumb.GetBitmap();
    x.Save(Response.OutputStream, ImageFormat.Jpeg);
    Response.ContentType = "image/jpeg";
   }
   catch (Exception ex)
   {
    Response.Write(ex.Message);
   }
  }
 }
}

Class file: GetImage. cs


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Web.UI;
namespace CaptureToImage
{
 public class GetImage
 {
  int S_Height;
  int S_Width;
  int F_Height;
  int F_Width;
  string MyURL;
  public int ScreenHeight
  {
   get
   {
    return S_Height;
   }
   set
   {
    S_Height = value;
   }
  }
  public int ScreenWidth
  {
   get
   {
    return S_Width;
   }
   set
   {
    S_Width = value;
   }
  }
  public int ImageHeight
  {
   get
   {
    return F_Height;
   }
   set
   {
    F_Height = value;
   }
  }
  public int ImageWidth
  {
   get
   {
    return F_Width;
   }
   set
   {
    F_Width = value;
   }
  }
  public string WebSite
  {
   get
   {
    return MyURL;
   }
   set
   {
    MyURL = value;
   }
  }
  public GetImage(string WebSite, int ScreenWidth, int ScreenHeight, int ImageWidth, int ImageHeight)
  {
   this.WebSite = WebSite;
   this.ScreenHeight = ScreenHeight;
   this.ScreenWidth = ScreenWidth;
   this.ImageHeight = ImageHeight;
   this.ImageWidth = ImageWidth;
  }
  [STAThread]
  public Bitmap GetBitmap()
  {
   WebPageBitmap Shot = new WebPageBitmap(this.WebSite, this.ScreenWidth, this.ScreenHeight);
   Shot.GetIt();
   Bitmap Pic = Shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
   return Pic;
  }
 }
 public class WebPageBitmap
 {
  WebBrowser MyBrowser;
  string URL;
  int Height;
  int Width;
  public WebPageBitmap(string url, int width, int height)
  {
   this.URL = url;
   this.Width = width;
   this.Height = height;
   MyBrowser = new WebBrowser();
   MyBrowser.ScrollBarsEnabled = false;
   MyBrowser.Size = new Size(this.Width, this.Height);
  }
  public void GetIt()
  {
   NavigateUrl(this.URL);
   while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
   {
    Application.DoEvents();
   }
  }
  public delegate void DelUserHandler(string url);
  public void NavigateUrl(string url)
  {
   try
   {
    if (this.MyBrowser.InvokeRequired)
    {
     DelUserHandler handler = new DelUserHandler(NavigateUrl);
     MyBrowser.Invoke(handler, url);
    }
    else
    {
     this.MyBrowser.Navigate(url);
    }
   }
   catch (Exception ex)
   {
    throw new Exception("NavigateUrl()" + ex.Message);
   }
  }
  public Bitmap DrawBitmap(int theight, int twidth)
  {
   Bitmap myBitmap = new Bitmap(this.Width, this.Height);
   Rectangle DrawRect = new Rectangle(0, 0, this.Width, this.Height);
   MyBrowser.DrawToBitmap(myBitmap, DrawRect);
   System.Drawing.Image imgOutput = myBitmap;
   System.Drawing.Bitmap oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
   Graphics g = Graphics.FromImage(oThumbNail);
   g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
   Rectangle oRectangle = new Rectangle(0, 0, twidth, theight);
   g.DrawImage(imgOutput, oRectangle);
   try
   {
    return oThumbNail;
   }
   catch
   {
    return null;
   }
   finally
   {
    imgOutput.Dispose();
    imgOutput = null;
    MyBrowser.Dispose();
    MyBrowser = null;
   }
  }
 }
}

PS: Reference System. Windows. Forms needs to be added to the project

I hope this paper is helpful to everyone's asp. net programming.


Related articles: