CopyCode The Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. drawing;
Namespace Chen
{
/// <Summary>
/// Handlerimageopener abstract description
/// </Summary>
Public class handlerimageopener: ihttphandler
{
Public handlerimageopener ()
{
//
// Todo: add the constructor logic here
//
}
Private string _ Path = "";
/// <Summary>
/// Watermark image path
/// </Summary>
Public String pngpath
{
Get
{
If (_ Path = "")
{
_ Path = system. Web. httpcontext. Current. server. mappath (configurationmanager. deleetpipeline ["watermarkedimagepath"]);
}
Return _ path;
}
}
/// <Summary>
/// Add a watermark to the image and write it to response. outputstream
/// </Summary>
/// <Param name = "HC"> context object </param>
Public void getnewbitmap (httpcontext HC)
{
// Load the original image
// System. Web. httpcontext. Current. response. Write (system. Drawing. image. fromfile (HC. Request. physicalpath ));
// System. Web. httpcontext. Current. response. End ();
Bitmap oldbmp = new Bitmap (system. Drawing. image. fromfile (HC. Request. physicalpath ));
Int newwidth = oldbmp. width;
Int newheight = oldbmp. height;
If (oldbmp! = NULL)
{
// Bind the canvas
Graphics grap = graphics. fromimage (oldbmp );
// Load the watermark image
Bitmap bt = new Bitmap (pngpath );
// Watermark Position Control
Int pH = getnewpoint (newheight, BT. Height, true );
Int PW = getnewpoint (newwidth, BT. Width, false );
If (newheight <Ph * 8)
PH = Ph/2;
If (newwidth <PW)
PW = pw/2/2;
Int PX = newheight-pH;
Int py = newwidth-PW-3;
// Add a watermark
GRAP. drawimage (BT, Py, PX, PW, pH );
// Write to the output stream
Oldbmp. Save (HC. response. outputstream, system. Drawing. imaging. imageformat. JPEG );
}
}
// Control width and height
Private int getnewpoint (INT oldp, int newp, bool ISW)
{
Int P = 4;
If (ISW)
{
P = 16;
}
If (oldp <(newp * p ))
{
Newp/= 2;
If (oldp <(newp * p ))
{
Getnewpoint (oldp, newp, ISW );
}
}
Return newp;
}
# Region ihttphandler Member
Bool ihttphandler. isreusable
{
Get {return true ;}
}
Void ihttphandler. processrequest (httpcontext context)
{
Getnewbitmap (context );
}
# Endregion
}
}
After the. dll file is generated, configure it in Web. config.
<! -- Watermark image path -->
<Deleetask>
<Add key = "watermarkedimagepath" value = "~ /Logo.gif "/>
</Appsettings>
<! -- Reference the processing function path to the directory of the image to be watermark -->
<Httphandlers>
<Add type = "Chen. handlerimageopener, Chen "verb =" * "Path =" image /*. JPG, image /*. GIF, image /*. PNG, image /*. BMP "/>
</Httphandlers>