Example of adding an image watermark to an image using asp. net,

Source: Internet
Author: User

Example of adding an image watermark to an image using asp. net,

Preface

This article mainly introduces asp. net to add an image watermark to an image. Note that it is an image watermark. For more information, see.

The method is as follows:

First, write a class for writing image watermarks. First, create an ImageWriter class library (which contains enumeration types and methods)

Using System; using System. collections. generic; using System. linq; using System. text; using System. IO; using System. collections; using System. drawing; using System. drawing. drawing2D; using System. drawing. imaging; namespace ImageWriter {// <summary> // image processing class // </summary> public class ImageManager {# region variable declaration start /// <summary> // Enumeration: watermark position /// </summary> public enum WatermarkPosition {// <summary> /// Upper Left /// </summary> LeftTop, /// <summary> /// middle Left /// </summary> Left, /// <summary> /// bottom left /// </summary> LeftBottom, /// <summary> /// Top right /// </summary> Top, /// <summary> /// Center /// </summary> Center, /// <summary> /// Bottom /// </summary> Bottom, /// <summary> /// top right /// </summary> RightTop, /// <summary> /// middle right /// </summary> RightCenter, /// <summary> /// bottom right /// </summary> RigthBottom} # endregio N variable declaration end # region constructor start // <summary> // constructor: Default /// </summary> public ImageManager () {}# endregion constructor end # region private function start // <summary> // get: Image De-extension (including the complete path and its file name) lowercase string /// </summary> /// <param name = "path"> image path (including the complete path, file name, and extension ): string </param> // <returns> return: lowercase string of the image extension (including the complete path and its file name): string </returns> private string GetFileName (string path) {return path. remove (path. lastIndexOf ('. ')). toLower ();} // <Summary> // obtain: The image is '. 'lower-case string name extension /// </summary> /// <param name = "path"> image path (including the complete path, file name, and extension ): string </param> // <returns> return value: the image is '. 'lower-case string extension: string </returns> private string GetExtension (string path) {return path. remove (0, path. lastIndexOf ('. ')). toLower () ;}/// <summary> // obtain: The image is '. 'System. drawing. imaging. imageFormat object // </summary> /// <param name = "format"> '. 'Lower-case string extension: string </param> // <returns> returns: The image starts '. 'System. drawing. imaging. imageFormat object: System. drawing. imaging. imageFormat </returns> private ImageFormat GetImageFormat (string format) {switch (format) {case ". bmp ": return ImageFormat. bmp; case ". emf ": return ImageFormat. emf; case ". exif ": return ImageFormat. exif; case ". gif ": return ImageFormat. gif; case ". ico ": return Imag EFormat. icon; case ". png ": return ImageFormat. png; case ". tif ": return ImageFormat. tiff; case ". tiff ": return ImageFormat. tiff; case ". wmf ": return ImageFormat. wmf; default: return ImageFormat. jpeg ;}/// <summary> // obtain: enumerative Uinatlex. toolBox. imageManager. system. drawing. rectangle object // </summary> // <param name = "positon"> enumerate Uinatlex. toolBox. imageManager. watermarkPosition: Uinatlex. toolBox. imageManager. watermarkPosition </param> /// <param name = "X"> source Image Width: int </param> /// <param name = "Y"> source Image Height: int </param> // <param name = "x"> watermark width: int </param> // <param name = "y"> watermark height: int </param> /// <param name = "I"> margin: int </param> /// <returns> return: enumerative Uinatlex. toolBox. imageManager. system. drawing. rectangle object: System. drawing. rectangle </returns> private Rectangle G EtWatermarkRectangle (WatermarkPosition positon, int X, int Y, int x, int y, int I) {switch (positon) {case WatermarkPosition. leftTop: return new Rectangle (I, I, x, y); case WatermarkPosition. left: return new Rectangle (I, (Y-y)/2, x, y); case WatermarkPosition. leftBottom: return new Rectangle (I, Y-y-I, x, y); case WatermarkPosition. top: return new Rectangle (X-x)/2, I, x, y); case Wat ErmarkPosition. center: return new Rectangle (X-x)/2, (Y-y)/2, x, y); case WatermarkPosition. bottom: return new Rectangle (X-x)/2, Y-y-I, x, y); case WatermarkPosition. rightTop: return new Rectangle (X-x-I, I, x, y); case WatermarkPosition. rightCenter: return new Rectangle (X-x-I, (Y-y)/2, x, y); default: return new Rectangle (X-x-I, y-y-I, x, y) ;}# endregion private letter Number of ends # region text generation starts # endregion text generation ends # region setting transparency starts /// <summary> /// set: image System. drawing. bitmap object transparency /// </summary> /// <param name = "sBitmap"> image System. drawing. bitmap object: System. drawing. bitmap </param> /// <param name = "transparence"> watermark transparency (the higher the value, the lower the transparency, and the range is 0.0f ~ Between 1.0f): float </param> // <returns> image System. drawing. bitmap: System. drawing. bitmap </returns> public Bitmap SetTransparence (Bitmap bm, float transparence) {if (transparence = 0.0f | transparence = 1.0f) throw new ArgumentException ("the transparency value can only be 0.0f ~ Between 1.0f "); float [] [] floatArray = {new float [] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float [] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float [] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float [] {0.0f, 0.0f, 0.0f, transparence, 0.0f }, new float [] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f }}; ImageAttributes imageAttributes = new ImageAttributes (); imageAttributes. setColorMatrix (new ColorMatrix (floatArray), ColorMatr IxFlag. default, ColorAdjustType. bitmap); Bitmap bitmap = new Bitmap (bm. width, bm. height); Graphics graphics = Graphics. fromImage (bitmap); graphics. drawImage (bm, new Rectangle (0, 0, bm. width, bm. height), 0, 0, bm. width, bm. height, GraphicsUnit. pixel, imageAttributes); graphics. dispose (); imageAttributes. dispose (); bm. dispose (); return bitmap;} // <summary> /// set: image System. drawing. bitmap object Transparency/ /// </Summary> /// <param name = "readpath"> image path (including the complete path, file name, and extension ): string </param> /// <param name = "transparence"> watermark transparency (the higher the value, the lower the transparency, and the range is 0.0f ~ Between 1.0f): float </param> // <returns> image System. drawing. bitmap: System. drawing. bitmap </returns> public Bitmap SetTransparence (string readpath, float transparence) {return SetTransparence (new Bitmap (readpath), transparence );} # endregion sets transparency to end # region adds a watermark to start /// <summary> // generation: System of the source image watermark. drawing. bitmap object // </summary> /// <param name = "sBitmap"> source image System. drawing. bitmap object: System. drawing. bi Tmap </param> /// <param name = "wBitmap"> watermark System. drawing. bitmap object: System. drawing. bitmap </param> /// <param name = "position"> enumerate Uinatlex. toolBox. imageManager. watermarkPosition: Uinatlex. toolBox. imageManager. watermarkPosition </param> /// <param name = "margin"> watermark margin: int </param> /// <returns> return: System of the source image watermark. drawing. bitmap object System. drawing. bitmap </returns> public Bitmap CreateWatermark (Bitmap sBit Map, Bitmap wBitmap, WatermarkPosition position, int margin) {Graphics graphics = Graphics. fromImage (sBitmap); graphics. drawImage (wBitmap, GetWatermarkRectangle (position, sBitmap. width, sBitmap. height, wBitmap. width, wBitmap. height, margin); graphics. dispose (); wBitmap. dispose (); return sBitmap;} # endregion adds watermark end # region image cutting start # endregion image cutting end # region image scaling start # endregion image scaling end # region saves image to file Start # region normal save start // <summary> // save: System. drawing. bitmap object to the image file /// </summary> /// <param name = "bitmap"> System. drawing. bitmap object: System. drawing. bitmap </param> /// <param name = "writepath"> Save path (including the complete path, file name, and extension): string </param> public void Save (Bitmap bitmap, string writepath) {try {bitmap. save (writepath, GetImageFormat (GetExtension (writepath); bitmap. dispose ();} catch {throw new Argumen TException ("image saving error") ;}/// <summary >/// save: object To the image file // </summary> // <param name = "readpath"> source image path (including the complete path, file name, and extension ): string </param> // <param name = "writepath"> Save path (including the complete path, file name, and extension): string </param> public void Save (string readpath, string writepath) {if (string. compare (readpath, writepath) = 0) throw new ArgumentException ("source image and target image address are the same"); try {Save (new Bitmap (readpath), writepath);} catch {Throw new ArgumentException ("image reading error ");}} # endregion normal save end # region text drawing save start # endregion text drawing save end # region transparency change save start // <summary> // save: sets the transparency of the object to the image file /// </summary> /// <param name = "sBitmap"> image System. drawing. bitmap object: System. drawing. bitmap </param> /// <param name = "transparence"> watermark transparency (the higher the value, the lower the transparency, and the range is 0.0f ~ Between 1.0f): float </param> // <param name = "writepath"> Save path (including the complete path, file name, and extension ): string </param> public void SaveTransparence (Bitmap bitmap, float transparence, string writepath) {Save (SetTransparence (bitmap, transparence), writepath );} /// <summary> /// save: set the image transparency to the image file /// </summary> /// <param name = "readpath"> source image path (including the complete path, file name, and extension ): string </param> /// <param name = "transparence"> watermark transparency (the higher the value, the lower the transparency, and the range is 0.0f ~ Between 1.0f): float </param> // <param name = "writepath"> Save path (including the complete path, file name, and extension ): string </param> public void SaveTransparence (string readpath, float transparence, string writepath) {Save (SetTransparence (readpath, transparence), writepath );} # endregion transparency change save end # region watermark image save start /// <summary> /// save: draw the watermark object to the image file /// </summary> /// <param name = "sBitmap"> source image System. drawing. bitmap object: System. drawing. bitmap </ Param> // <param name = "wBitmap"> watermark System. drawing. bitmap object: System. drawing. bitmap </param> /// <param name = "position"> enumerate Uinatlex. toolBox. imageManager. watermarkPosition: Uinatlex. toolBox. imageManager. watermarkPosition </param> /// <param name = "margin"> watermark margin: int </param> // <param name = "writepath"> Save path (including the complete path, file name, and extension): string </param> public void SaveWatermark (Bitmap sBitmap, bitmap wBitmap, Water MarkPosition position, int margin, string writepath) {Save (CreateWatermark (sBitmap, wBitmap, position, margin), writepath);} // <summary> // Save: draw the watermark object to the image file /// </summary> /// <param name = "readpath"> image path (including the complete path, file name, and extension ): string </param> // <param name = "watermarkpath"> watermark image path (including the complete path, file name, and extension ): string </param> /// <param name = "transparence"> watermark transparency (the higher the value, the lower the transparency, and the range is 0.0f ~ Between 1.0f): float </param> // <param name = "position"> enumeration Uinatlex. toolBox. imageManager. watermarkPosition: Uinatlex. toolBox. imageManager. watermarkPosition </param> /// <param name = "margin"> watermark margin: int </param> // <param name = "writepath"> Save path (including the complete path, file name, and extension): string </param> public void SaveWatermark (string readpath, string watermarkpath, float transparence, WatermarkPosition position, int margin, string writepath) {if (string. compare (readpath, writepath) = 0) throw new ArgumentException ("source image and target image address are the same"); if (transparence = 0.0f) Save (readpath, writepath ); else if (transparence = 1.0f) SaveWatermark (new Bitmap (readpath), new Bitmap (watermarkpath), position, margin, writepath); else SaveWatermark (new Bitmap (readpath ), setTransparence (watermarkpath, transparence), position, margin, writepath );} # endregion watermark image save end # region image cut save start # endregion image cut save end # region Image zoom save start # endregion Save image to end of file }}/* the call is simple. saveWatermark (source image address, watermark address, transparency, watermark position, margin, save position); Uinatlex. toolBox. imageManager im = new Uinatlex. toolBox. imageManager (); im. saveWatermark (Server. mapPath ("/ .jpg"), Server. mapPath ("/ .jpg"), 0.5f, Uinatlex. toolBox. imageManager. watermarkPosition. rigthBottom, 10, Server. mapPath ("/ .jpg "));*/

Call the above class on the client page

Protected void Page_Load (object sender, EventArgs e) {string newfileName = Server. mapPath ("img/") + DateTime. now. millisecond + "" + DateTime. now. second + ". jpg "; ImageManager im = new ImageManager (); // call im. saveWatermark (source image address, watermark address, transparency, watermark position, margin, saved location); im. saveWatermark (Server. mapPath ("img/Chrysanthemum.jpg"), Server. mapPath ("img/woshi.jpg"), 0.9f, ImageManager. watermarkPosition. rigthBottom, 10, newfileName );}

Summary

Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.

Related Article

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.