asp.net inheritance IHttpHandler interface to add a watermark function to the website image practical skills

Source: Internet
Author: User
Tags inheritance

The example of this article describes the ASP.net inheritance IHttpHandler interface implementation to the Web site image add watermark function. Share to everyone for your reference, specific as follows:

First show the picture effect:

1. Add the class file under App_Code, named Imagesy file as follows

public class Imagesy:ihttphandler {public Imagesy () {////todo: Add constructor logic//} #region Ihttphan
  Dler member public bool IsReusable {get {true;} The public void ProcessRequest (HttpContext context) {//Gets the requested physical picture path string ImagePath = Context.
    Request.PhysicalPath;
    System.Drawing.Image Image = null;
      if (file.exists (ImagePath)) {//define watermark text string text = "This picture comes to my Site";
      Define watermark text font size int fontsize = 22;
      Watermark text Fonts Font font = new Font ("XXFarEastFont-Arial", fontsize);
      Load picture image by Physical address of picture = System.Drawing.Image.FromFile (ImagePath);
      Graphics g = graphics.fromimage (image);
      Gets the display area size required to draw the watermark text SizeF size = g.measurestring (text, font); if (size. Width > Image. Width | | Size. Height > Image.
        Height) {} else {Brush Brush = brushes.red; g.DrawString (text, font, brush, image. Width-size. Width, image. Height-size.
        Height);
  G.dispose ();    } else {} image. Save (context.
  Response.outputstream, Imageformat.jpeg);

 } #endregion}

2. Configure Webconfig, add location new node

<location path= "Images" >
 <system.web>
   
 

3. Test, new ASPX page, display picture, Watermark will automatically add the

More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."

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.