C # Add a watermark to an image of a certain format at the specified position on the site

Source: Internet
Author: User

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>

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.