Asp.net Image Watermarking code

Source: Internet
Author: User

<% @ Webhandler language = "c #" class = "bookhandler" %>

Using system;

Using system. web;

Using system. drawing;

Using system. io;

 

Public class bookhandler: ihttphandler {

Private const string coversaddr = "~ /Image/"; // cover folder path

Private const string watermark_url = "~ /Images/watermark.jpg "; // quantity

Watermark path

Private const string default_url = "~ /Images/default.jpg "; // default image

Film path

Public void processrequest (httpcontext context ){

String path = context. request. mappath (coversaddr +

Context. request. params ["isbn"]. tostring () + ". jpg ");

Image cover;

If (file. exists (path ))

{

Cover = image. fromfile (path );

Image watermark = image. fromfile (context. request. mappath

(Watermark_url ));

Graphics g = graphics. fromimage (cover );

G. drawimage (watermark, new rectangle (cover. width-

Watermark. width, cover. height-watermark. height, watermark. width,

Watermark. height), 0, 0, watermark. width, watermark. height,

Graphicsunit. pixel );

G. dispose ();

Watermark. dispose ();

}

Else

{

Cover = image. fromfile (context. request. mappath

(Default_url ));

}

 

Context. request. contenttype = "image/ipeg ";

Cover. save

(Context.response.outputstream,system.drawing.imaging.imageformat.jpeg );

Cover. dispose ();

Context. response. end ();

}

 

Public bool isreusable {

Get {

Return false;

}

}

 

}

Finally, change the path of the connected image

Bookcover. ashx? Isbn = number.

 

Configure jpg access in iis

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.