Using the common class under the system.drawing Namespace in. We can easily add text watermarks and pictures to the image, and can customize the size, position, color, transparency of the watermark, etc.! For pictures such as JPG or PNG, you can get the Graphics object directly through Graphics G = graphics.fromimage (image), but for GIF images, you cannot obtain Graphics objects through this method, and the method of solving this problem is based on the .gif file The size generates a bitmap pattern, then copy the original map to the pattern area, do it, the following is the full code of the watermark adds classes:
Using
System;
Using
System.drawing;
Using
System.drawing.Image;
Using
System.IO;
Using
System.drawing.drawing2d;
Namespace
Jillzhang
{Public enum typepe {text, image} / ** //// /// Add water printed to the image Description /// summary> public class watermark {--------- ------------ Member variable --------------------- # Region ------------- -------- Member variable -------------------- private string _text = "; private string _imgpath ="; private int _markx = 0 ; private int _markY = 0; private float _transparency = 1; private string _fontFamily = "Arial"; private Color _textColor = Color.Black; private bool _textbold = false; int [] sizes = new int [] {48,32,16 , 8, 6, 4}; private image _image = null; private image _markediamge = null; private marktype _marktype = marktype.text; #ENDREGON -------------------------------------------------------------------------------------------------------------- - Constructor -------------------- # Region --------------------- Constructor - ------------------- / ** //// /// Instantiate a watermark /// summary> public watermark () { } / ** //// /// Improve a text water printed instance /// summary> /// Watermark text param> /// text font param> /// Whether bold param> /// font color param> /// tag location horizontal parame> /// Tag position ordinate param> public watermark (String text, string fontfamily, bool bold, color color, int markx, int marky) {this._marktype = marktype .TEXT; THIS._TEXT = TEXT;
THIS._FONTFAMILY = FontFamily; this._textbold = bold; this._textColor = color; this._markx = markx; this._marky = mark; this.mark ();} ///////// ///////// Instantiate a picture of water printed instance /// summary> /// Watermark picture path param> /// Transparency param> /// Tag location horizontal param> /// tag position ordinate param> public Watermark (String ImagePath, Float TranParence, int markx, INT MARKY) {this._marktype = marktype.image; this._imgpath = imagePath; this._markx = markx; this._marky = mark; this._transparency = TRANPARENCE; this.mark ();} #ENDREGION ----- ---------------- Public attribute -------------------- # Region --------- ------------ Public property ------------------- / ** //// /// watermark type /// summary> public marktype watermarktype { Get {return_marktype;} set {_marktype = value;}} / ** //// /// text watermark content /// summary> public string text {get {return_text;} set { _Text = value;}} / ** //// /// Watermark Image Get Path //////////////////////////////////>
} Set {this._imgpath = value;}} / ** //// /// watermark picture /// summary> public image water means.fromfile (this.waterImagePath) Catch {return null;}}} / ** //// /// Add watermark location horizontal /// summary> public int markx {get {return_markx;} set {_markx = Value;}} / ** //// /// Add Watermark position Accounts /// summary> public int marky {get {return_marky;} set {_marky = value;}} / * * //// /// Water Print Transparency /// summary> public float transparency {get {f (_Transparency> 1.0F) { _Transparency = 1.0F;} return _transparency;} set {_transparency = value;}} / ** //// /// Watermark word color /// summary> public color textcolor {get {return_textColor } Set {_textColor = value;}} / ** //// /// Watermark text 5/// summary> public string textFontFamilyStr {get {return_fontfamily;} set {_fontfamily = value; }} Public fontfamily textfontfamily {get {return new fontfamily (THISTEXTFONTFAMILYSTR);
}} / ** //// /// Watermark text to bold /// summary> public bool bold {get {return_textbold;} set {_textbold = value;}} / ** // // /// original /// summary> public image sourceImage {get {return _image;} set {_image = value;}} / ** //// /// After the watermark, you can map /// summary> public image markedimage {get {returnimage {get {return this._markediamge;}} #ENDREGON -------------------- Method Event - -------------------- # Region -------------------- - Method Event ----- ---------------- Public Image Mark (Image Img, MarkType MarkType, String Text, Image Waterimg, Int Markx, Int Marky, Bool Bold, Color TextColor, Float Transparence, Fontfamily Fontfamily {// first determine if the image is gif animation, if the GIF animation does not change the picture foreach (Guid guid in img.FrameDimensionsList) {FrameDimension dimension = new FrameDimension (guid); if (img.GetFrameCount (dimension)> 1) {return img;}} try {// add text watermark if (markType == MarkType. TEXT) {// Generate a new Bitmap object according to the source image as a pattern area, in order to add watermarking to the GIF picture, there is this turnover Bitmap newbitmap = new bitmap (img.width, img.height, pixelformat.format24bpprgb); // Set new bitmap.SetResolution (img.vertageresolution); img.vertageresolution;
// Create a Graphics object to operate graphics g = graphics.FromImage (newbitmap); // Eliminate serrated g.SmoothingMode = SMOOTHINGMODE.Antialias; // Copy the original map to the status area g.drawImage (IMG New Rectangle (0,0, img.width, img.height), 0, 0, img.width, img.height, graphicsunit.pixel); // declared font object font cfont = null; // Used to test watermark Text length scales sizef size = new sizef (); // detect a suitable picture size font size to accommodate watermark text size adaptive for FOR (INT i = 0; i <6; i ) {// creation one Font object cfont = new font (fontfamily, sizes [i]); // Whether bold if (! Bold) {cfont = new font (fontfamily, sizes [i], fontstyle.regary);} else {cfont = new font (FontFamily, Sizes [I], FontStyle.b OLD);} // Measuring text size size = g.MeasureString (this.text, cfont); // Match the first matching requirements Font size if (USHORT) size.width <(usort) img.width) { Break;}} // Create a brush object, ready to write text brush brush = new solidbrush (TextColor); // Write text G. DrawString (Text, CFont, Brush, Markx, Marky); / / Release Graphics object g.dispose ();
// Read the generated picture to MemoryStream System.io.MemoryStream ms = new system.io.MemoryStream (); newbitmap.save (ms, imageformat.jpeg); // Recons into image object img = system.drawing.image. FromStream (ms); // Returns new image object return;} // Add Image Watermark Else if (MarkType == MarkType.Image) {// Get Watermark Image Image Markimg = WaterImg; // Create Color Matrix Float [] [] PTSArray = {new float [] {1, 0, 0, 0, 0}, new float [] {0, 1, 0, 0, 0}, new float [] {0, 0, 1, 0, 0}, new float [] {0, 0, 0, transparence, 0}, // Note: This is 0.0F is completely transparent, 1.0F is completely opaque new float [] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix (ptsArray); // Create a property Image ImageAttributes imageAttributes = new ImageAttributes (); // add to the color matrix properties imageAttributes.SetColorMatrix (colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default); // bit map is generated mapping area Bitmap newBitmap = new Bitmap (img.Width, img.Height, PixelFormat.Format24bppRgb); // set the resolution newBitmap.SetResolution (img.HorizontalResolution, img.VerticalResolution); // Create Graphics Graphics g = Graphics.FromImage NEWBITMAP;
// Eliminate serrated g.SmoothingMode = smoothingmode.antialias; // Copy the original map to the dynamic zone G.DrawImage (IMG, New Rectangle (0, Img.Width, Img.Height), 0, 0, IMG.WIDTH Img.height, graphicsunit.pixel; // If the original picture is too small if (Markimg.Width> img.width || Markimg.Height> img.height) {system.drawing.image.getthumbnailimageabort callb = null; // Generate thumbnails to the watermark image, zoom to the original image 1/4 system.drawing.Image new_img = markimg.getthumbnailimage (img.width / 4, markimg.Height * img.width / markimg.width, callb, new system.intptr ()); // Add Watermark G.drawImage (NEW_IMG, New Rectangle (Markx, Marky, New_img.Width, New_img.Height), 0, 0, New_img.width, New_img.Height, GraphicsUnit.pixel, ImageAttributes; / / Release thumbnail new_img.dispose (); // Release graphics g.dispose (); // will generate a picture read MemoryStream System.io.MemoryStream MS = new system.io.MemoryStream (); newbitmap.save (ms, imageformat.jpeg); // Return new Image object img = image.fromstream (ms); return img;} // The original map is large enough ELSE {// Add Watermark G. DrawImage (Markimg, New Rectangle (Markx, Marky, Markimg.Width, Markimg.Height), 0, 0, Markimg.Width, Markimg.Height, GraphicsUnit.pixel, ImageAttributes ; // Release Graphics g.dispose ();
// Read the generated picture to read the MemoryStream System.io.MemoryStream ms = new system.io.MemoryStream (); newbitmap.save (ms, imageformat.jpeg); // Return to the new image object img = image.fromstream (MS ); Return img;}} Return IMG;} catCH}} / ** //// /// Add watermark, this method is suitable for GIF formatted pictures /// summary> / // Need to add water print pictures param> /// After adding watermark, you can get pictures returns> private void mark () {this._markediamge = mark (this.SourceImage, This.WatermarkType, this._text, this.waterimage, this._markx, this._marky, this._textbold, this._textcolor, this._transparency, this.TextFontFamily;} #endregion}} Utilize this class, you can easily Add watermarks to the picture, but there is not enough, to add watermarks through this method, will destroy the .gif animation, because I don't know if it is to judge whether it is an animation, this problem also requires the recommendations and help of friends.