The method used here is to add a watermark During User Access (without damaging the original image, but affecting server performance ).
(1) Use the general processing program bookcover. asxh
Using system;
Using system. Web;
Using system. drawing;
Using system. IO;
Public class bookcover: ihttphandler {
// Cover folder path
Private const string coversaddr = "~ /Images/bookcovers /";
// Digital watermark path
Private const string watermarkaddr = "~ /Images/watermark.jpg ";
// Default image path
Private const string defaultimage = "~ /Images/default.jpg ";
Public void processrequest (httpcontext context ){
// Path of the combined Image
String Path = context. Request. mappath (coversaddr + context. Request. Params ["ISBN"]. tostring () + ". jpg ");
System. Drawing. Image image;
// Determine whether a file exists in the request's physical path
If (file. exists (PATH ))
{
// Load the file
Image = image. fromfile (PATH );
// Load the watermark image
Image Watermark = image. fromfile (context. Request. mappath (watermarkaddr ));
// Instantiate the canvas
Graphics G = graphics. fromimage (image );
// Draw a watermark on the Image
G. drawimage (watermark, new rectangle (image. width-watermark. width, image. 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
Image = image. fromfile (context. Request. mappath (defaultimage ));
}
// Set the output format
Context. response. contenttype = "image/JPEG ";
// Save the image to the output stream
Image. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );
Image. Dispose ();
Context. response. End ();
}
Public bool isreusable {
Get {
Return false;
}
}
}
Then, you need to change the access path of all the images you want to add the watermark to: "bookcover. asxh? ISBN = image No ".
(2) Use the global httphandler Method
You can use the watermark function without changing the image access path. Put the code file in the app_code directory:
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;
}
}
}
Modify the configuration file and add the following content:
<Httphandlers>
<Add verb = "*" Path = "images/bookcovers/*. jpg" type = "coverhandler"/>
</Httphandlers>
All JPG file requests under the images/bookcovers/directory are sent to the coverhandler class for processing.
In addition, if you publish a website, you need to modify the IIS application configuration, as shown in:
After the configuration is complete, we recommend that you restart IIS.