Merge two images (watermark effect)

Source: Internet
Author: User

Define a global class file in the app_code class

Using system;
Using system. Web;
Using system. drawing;
Using system. Drawing. imaging;
Using system. IO;

/// <Summary>
/// Digital watermark with the extension of the ing File
/// </Summary>
Public class coverhandler: ihttphandler
{
Private const string watermark_url = "~ /Images/watermark.jpg "; // watermark image
Private const string defaultimage_url = "~ /Images/default.jpg "; // default image
Public coverhandler ()
{
}
Public void processrequest (httpcontext context)
{
System. Drawing. Image cover;
// Determine whether a file exists in the request's physical path
If (file. exists (context. Request. physicalpath ))
{
// Load the file
Cover = image. fromfile (context. Request. physicalpath );
// Load the watermark image
Image Watermark = image. fromfile (context. Request. mappath (watermark_url ));
// Instantiate the canvas
Graphics G = graphics. fromimage (cover );
// Draw a watermark on the Image
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 );
// Release the canvas
G. Dispose ();
// Release the watermark image
Watermark. Dispose ();
}
Else
{
// Load the default image
Cover = image. fromfile (context. Request. mappath (defaultimage_url ));
}
// Set the output format
Context. response. contenttype = "image/JPEG ";
// Save the image to the output stream
Cover. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );
Cover. Dispose ();
Context. response. End ();
}
Public bool isreusable
{
Get
{
Return false;
}
}
}

Code in the webconfig file:

<System. Web>
<Httphandlers>
<Add verb = "*" Path = "images/bookcovers/*. jpg" type = "coverhandler"/>
</Httphandlers>
</System. Web>

 

In this way, when you call all the images set in the path attribute in Web. config, the watermark is automatically added!

 

 

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.