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!