Asp. NET implementation of Web page thumbnails based on the URL method _ Practical skills

Source: Internet
Author: User

The example in this article describes the method by which the ASP.net implementation generates a Web page thumbnail based on the URL. Share to everyone for your reference, specific as follows:

Work needs to be used to generate Web page thumbnails based on the URL function, ready in advance.

On the internet to find a source code, but there are errors: the current thread is not in a single-threaded apartment, so the ActiveX control can not instantiate the "8856f961-340a-11d0-a9", resolved to run well after the record in this spare!

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 ">

<%@ 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 ">
 
 

PS: The red font is part of the code to solve the error increase, forcing the program to run in a single thread environment!

Call 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,);
    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); The public void GetIt () {NavigateUrl (.
   URL);
   while (mybrowser.readystate!= webbrowserreadystate.complete) {application.doevents ();
  } public delegate void Deluserhandler (string url); public void NavigateUrl (string url) {try {if. 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: Need to add references in Project System.Windows.Forms

I hope this article will help you to ASP.net program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.