Httphandler: Add a watermark to the image in the specified path.

Source: Internet
Author: User
For the project to be handed over to accp5.0 certification on Christmas day 25, there is a requirement that the cover of all books in the bookstore should be placed under \ images \ convers, all the pictures under the referenced path must be added with the store name watermark image of the bookstore. That is to say, the HTTP request is intercepted. Naturally, it can be done by httphandler. In consideration, the effect should be as follows: for general purpose, the monitored path, the watermark image path, and the default image Path 3 should be set in the configuration file for ease of modification; all images in the monitored path must have watermarks as long as they exist physically. If they do not exist physically, use the default image instead. If the image does not exist, use text instead. When accessing images in other paths, they should be normally displayed without watermarks... Code For debugging convenience, add the debugging option when compiling the httphandler class and reference this DLL in the project (compile: CSC/T: Library watermarkhandler. CS/debug) Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;

Namespace Xumh
{
/**/ ///   <Summary>
/// Image processing module: adds watermark output to images in a specified path
/// 1. The monitored directory path, watermark image path, and default image are all set in the web. config file.
/// 2. The requested image path does not belong to the monitoring path, and the image is output directly.
/// 3. The requested image path belongs to the monitoring path. If the image does not exist, the default image is displayed without watermarks.
/// 4. The requested image path belongs to the monitoring path and the file exists. Add a watermark.
/// Watermark image: Image + watermark image, output
/// Watermark image does not exist: Image + watermark text, output
///
/// 5. The web. config section is as follows:
/// <Deleetask>
/// <! -- Path to be monitored -->
/// <Add key = "monitorpath" value = "/images/bookcovers/"/>
/// <! -- Relative path of the default album art image displayed when the album art does not exist -->
/// <Add key = "defaultimage" value = "Images \ default.jpg"/>
/// <! -- Relative path of the watermark image relative to the root path of the Physical application -->
/// <Add key = "watermarkimage" value = "Images \ watermark.jpg"/>
/// </Appsettings>
///
/// <Httphandlers>
/// <Add verb = "*" Path = "*. jpg" type = "xumh. watermarkhandler, watermarkhandler"/>
/// </Httphandlers>
/// Xu Ming will debug the program on Visual Studio 2005
///   </Summary>
Public   Class Watermarkhandler: ihttphandler
{
Public   Bool Isreusable
{
Get {Return True;}
}

Public   Void Processrequest (httpcontext context)
{
String URL = Context. Request. url. absoluteuri. tolower ();
String Monitorpath = System. configuration. configurationmanager. configurettings [ " Monitorpath " ];
// If the image does not belong to the cover, output the image directly: if the image does not exist, do not process it.
Bool Isinteresturl = URL. Contains (monitorpath );
System. Drawing. Image imgsource =   Null ;
If ( ! Isinteresturl)
{
If ( ! System. Io. file. exists (context. Request. physicalpath ))
Return ;
Imgsource = System. Drawing. image. fromfile (context. Request. physicalpath );
Imgsource. Save (context. response. outputstream, imgsource. rawformat );
Imgsource. Dispose ();
Return ;
}  
// The image is a block, but the image imagesourcedoes not exist. The default image default.jpg is displayed.
String Physicalpath = Context. Request. physicalpath; // Context. server. mappath (URL );
If ( ! System. Io. file. exists (physicalpath ))
{
String Defaultimage = System. configuration. configurationmanager. configurettings [ " Defaultimage " ];
Imgsource = System. Drawing. image. fromfile (context. Request. physicalapplicationpath + Defaultimage );
Imgsource. Save (context. response. outputstream, imgsource. rawformat );
Imgsource. Dispose ();
Return ;
}
// -- ===-------------------- Add a watermark to the image as long as there is a cover image -------------------- = ---
String Watermarkimage = System. configuration. configurationmanager. configurettings [ " Watermarkimage " ];
String Watermarkimagepath = Context. Request. physicalapplicationpath + Watermarkimage;
Bool Bwatermarkimageexist = System. Io. file. exists (watermarkimagepath );
Imgsource = System. Drawing. image. fromfile (physicalpath );
System. Drawing. Graphics graphic = System. Drawing. Graphics. fromimage (imgsource );
If (Bwatermarkimageexist) // "Watermark image": If yes, the cover is a mixed watermark image.
{
System. Drawing. Image imgwatermark = System. Drawing. image. fromfile (watermarkimagepath );
Graphic. drawimage (imgwatermark,
Imgsource. Width - Imgwatermark. Width, imgsource. Height - Imgwatermark. height,
Imgwatermark. Width, imgwatermark. Height );
// Save the image to the output stream
Imgsource. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );
Imgwatermark. Dispose ();
}
Else // "Watermark image" does not exist: output text
{
System. Drawing. Font font =   New System. Drawing. Font ( " Youyuan " , 13.0f , System. Drawing. fontstyle. Bold );
Graphic. drawstring ( " The Third Wave + bookstore " , Font, system. Drawing. Brushes. Red, New System. Drawing. pointf ());
Imgsource. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );
}
Imgsource. Dispose ();
Graphic. Dispose ();
// Indicates JPG type: if it is not marked, ie is normal, but Firefox will contain garbled characters.
Context. response. contenttype =   " Image/JPEG " ;
Context. response. Flush ();
Context. response. End ();
}
}
}

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.