Step 1: Add a general Handler (Handler). This example is ImageHandler.
Using System; using System. data; using System. configuration; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. net. mime; using System. IO; using System. drawing; using System. drawing. imaging; using System. drawing. drawing2D ;////// Summary description for ImageHandler ///Public class ImageHandler: IHttpHandler {public ImageHandler () {} public string GetContentType (String path) {switch (Path. getExtension (path) {case ". bmp ": return" Image/bmp "; case ". gif ": return" Image/gif "; case ". jpg ": return" Image/jpeg "; case ". png ": return" Image/png "; default: break;} return String. empty;} public ImageFormat GetImageFormat (String path) {switch (Path. getExtension (path). ToLower () {case ". bmp ": return ImageFormat. bmp; case ". gif ": return ImageFormat. gif; case ". jpg ": return ImageFormat. jpeg; case ". png ": return ImageFormat. png; default: return null;} protected byte [] WatermarkImage (HttpContext context) {byte [] imageBytes = null; if (File. exists (context. request. physicalPath) {// Normally you 'd put this in a config file somewhere. string watermark = "Shifu detection "; Image image = Image. FromFile (context. Request. PhysicalPath); Graphics graphic; if (image. PixelFormat! = PixelFormat. Indexed & image. PixelFormat! = PixelFormat. Format8bppIndexed & image. PixelFormat! = PixelFormat. Format4bppIndexed & image. PixelFormat! = PixelFormat. format1bppIndexed) {// Graphic is not a Indexed (GIF) image graphic = Graphics. fromImage (image);} else {/* Cannot create a graphics object from an indexed (GIF) image. * So we're re going to copy the image into a new bitmap so * we can work with it. */Bitmap indexedImage = new Bitmap (image); graphic = Graphics. fromImage (indexedImage); // Draw the contents of the original bitmap The new bitmap. graphic. drawImage (image, 0, 0, image. width, image. height); image = indexedImage;} graphic. smoothingMode = SmoothingMode. antiAlias & SmoothingMode. highQuality; Font myFont = new Font ("Arial", 15); SolidBrush brush = new SolidBrush (Color. fromArgb (255, Color. red);/* This gets the size of the graphic so we can determine * the loop counts and placement of the watermarked text. */Siz EF textSize = graphic. measureString (watermark, myFont); // Write the text into ss the image. // for (int y = 0; y <image. height; y ++) // {// for (int x = 0; x <image. width; x ++) // {// PointF pointF = new PointF (x, y); // graphic. drawString (watermark, myFont, brush, pointF); // x + = Convert. toInt32 (textSize. width); //} // y + = Convert. toInt32 (textSize. height); //} // Write the text at the righ T bottom of the image. for (int y = image. height-25; y <image. height; y ++) {for (int x = image. width-100; x <image. width; x ++) {PointF pointF = new PointF (x, y); graphic. drawString (watermark, myFont, brush, pointF); x + = Convert. toInt32 (textSize. width);} y + = Convert. toInt32 (textSize. height);} using (MemoryStream memoryStream = new MemoryStream () {image. save (memoryStream, GetImageFormat (co Ntext. request. physicalPath); imageBytes = memoryStream. toArray () ;}} return imageBytes;} # region IHttpHandler Members public bool IsReusable {get {return false ;}} public void ProcessRequest (HttpContext context) {context. response. clear (); context. response. contentType = GetContentType (context. request. physicalPath); byte [] imageBytes = WatermarkImage (context); if (imageBytes! = Null) {context. response. outputStream. write (imageBytes, 0, imageBytes. length);} else {// No bytes = no image which equals no file. // Therefore send a 404-not found response. context. response. statusCode = 404;} context. response. end () ;}# endregion}
Step 2: Add the following code in web. config:
Step 3: debug the red word "Shifu detection" is displayed in the lower right corner of each image. For details, see the following link:
Click Open Link