The ihttphandler principle has been described in the article "ihttphandler URL rewriting". If you do not understand it, refer to this document.Article.
This document describes how to watermark a website image. The same principle lies in the configuration of Web. config and the GDI + operation.
First, configure web. config as follows:
<Httphandlers>
<Add verb = "*" Path = "*. jpg" type = "myhttphandler. watermarkhandler, myhttphandler"/>
For the same reason, see the article "ihttphandler URL rewriting". When the path is "* .jpg.pdf, for example, “logo.gif", it is processed by myhttphandler. watermarkhandler and then returned to the client.
Let's take a look at the implementation of watermarkhandler:
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Web;
Using System. drawing;
Using System. Drawing. imaging;
Using System. IO;
namespace myhttphandler
{< br> class watermarkhandler: ihttphandler
{< br> # region ihttphandler member
Public bool isreusable
{< br> Get { return true ;}
}
Public VoidProcessrequest (httpcontext context)
{
Commondeal. dealimagerequest (context );
}
# Endregion
}
}
The implementation of commondeal is as follows:
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Web;
Using System. Web. UI;
Using System. IO;
Using System. drawing;
Using System. Drawing. imaging;
Namespace Myhttphandler
{
Internal Class Commondeal
{
Public Static Void Dealimagerequest (httpcontext context)
{
Font enfont = New Font ( " Times New Roman " , 12 );
Memorystream MS= NewMemorystream ();
String Filepath = Context. server. mappath ( " Image " + Context. Request. Path );
Try
{
Image = Image. fromfile (filepath );
If (Ispixelformatindexed (image. pixelformat ))
{
Bitmap BMP = New Bitmap (image. Width, image. Height, pixelformat. format32bppargb );
Using (Graphics graphic = Graphics. fromimage (BMP ))
{
Graphic. interpolationmode = System. Drawing. drawing2d. interpolationmode. highqualitybicubic;
Graphic. smoothingmode = System. Drawing. drawing2d. smoothingmode. highquality;
Graphic. compositingquality = System. Drawing. drawing2d. compositingquality. highquality;
Graphic. drawimage (image, 0 , 0 );
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. lightgray ), 2 , 2 );
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. Black ), 0 , 0 );
}
BMP. Save (MS, system. Drawing. imaging. imageformat. GIF );
BMP. Dispose ();
}
Else
{
Using (Graphics graphic = Graphics. fromimage (image ))
{
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. lightgray ), 2 , 2 );
Graphic. drawstring ( " Pangxiaoliang " , Enfont, New Solidbrush (color. Black ), 0 , 0 );
}
bitmap thebitmap = New Bitmap (image );
thebitmap. save (MS, system. drawing. imaging. imageformat. JPEG);
thebitmap. dispose ();
}
Context. response. clearcontent ();
Context. response. contenttype= "Image/jpg";
Context. response. binarywrite (Ms. toarray ());
}
Catch
{
Bitmap Error = New Bitmap (context. server. mappath ( " Image/error.bmp " ));
Error. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Error. Dispose ();
Context. response. clearcontent ();
Context. response. contenttype = " Image/jpg " ;
Context. response. binarywrite (Ms. toarray ());
}
Finally
{
Ms. Close ();
Ms. Dispose ();
Context. response. End ();
}
}
Public Static Void Returnerrorimage (httpcontext context)
{
Memorystream MS = New Memorystream ();
Try
{
Bitmap Error = New Bitmap (context. server. mappath ( " Error.bmp " ));
Error. Save (MS, system. Drawing. imaging. imageformat. JPEG );
Error. Dispose ();
Context. response. clearcontent ();
Context. response. contenttype = " Image/jpg " ;
Context. response. binarywrite (Ms. toarray ());
}
Catch
{
}
Finally
{
Ms. Close ();
Ms. Dispose ();
Context. response. End ();
}
}
/// <Summary>
/// The pixelformat of the graphics exception is generated.
/// </Summary>
Private Static Pixelformat [] indexedpixelformats = {Pixelformat. undefined, pixelformat. dontcare,
Pixelformat. format16bppargb1555, pixelformat. format1bppindexed, pixelformat. format4bppindexed,
Pixelformat. format8bppindexed
};
/// <Summary>
/// Determines whether the pixelformat of the image is in the pixelformat that causes an exception.
/// </Summary>
/// <Param name = "imgpixelformat"> Pixelformat of the original image </Param>
/// <Returns> </returns>
Private Static Bool Ispixelformatindexed (pixelformat imgpixelformat)
{
Foreach (Pixelformat pf In Indexedpixelformats)
{
If (PF. Equals (imgpixelformat )) Return True ;
}
Return False ;
}
}
}
1) string filepath = context. server. mappath ("image" + context. Request. Path); first obtain the absolute path of the image on the server, so that the image can be processed.
2) memorystream MS = new memorystream (); memory stream is used to respond to client requests and return the processed image to the client.
3) Use GDI + to write a name on the image and return the new image to the client as a stream.