C. NET Image Watermark Adding Code

  • 2021-10-27 08:35:31
  • OfStack

This article example for everyone to share C #. NET to add image watermarking method, for your reference, the specific content is as follows


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Pub.Class
{
 /// <summary>
 ///  Add a watermark class   Only image watermarking is supported 
 /// </summary>
 public class Watermark
 {
  #region  Private member 
  private string modifyImagePath=null;
 private string drawedImagePath=null;
 private int rightSpace;
 private int bottoamSpace;
 private int lucencyPercent=70;
 private string outPath=null;
  #endregion

  #region  Constructor 
  /// <summary>
  ///  Constructor 
  /// </summary>
  public Watermark() { }
  #endregion

  #region  Attribute 
  /// <summary>
 ///  Gets or sets the image path to modify 
 /// </summary>
 public string ModifyImagePath
 {
 get{return this.modifyImagePath;}
 set{this.modifyImagePath=value;}
 }
 /// <summary>
 ///  Gets or sets the picture path in the drawing ( Watermark image )
 /// </summary>
 public string DrawedImagePath
 {
 get{return this.drawedImagePath;}
 set{this.drawedImagePath=value;}
 }
 /// <summary>
 ///  Gets or sets the right margin of the watermark in the modified picture 
 /// </summary>
 public int RightSpace
 {
 get{return this.rightSpace;}
 set{this.rightSpace=value;}
 }
 /// <summary>
  ///  Gets or sets the height of the watermark from the bottom in the modified picture 
 /// </summary>
 public int BottoamSpace
 {
 get{return this.bottoamSpace;}
 set{this.bottoamSpace=value;}
 }
 /// <summary>
 ///  Gets or sets the transparency of the watermark to draw , Note that it is the percentage of transparency of the original picture 
 /// </summary>
 public int LucencyPercent
 {
 get{return this.lucencyPercent;}
 set { if(value>=0&&value<=100) this.lucencyPercent=value; }
 }
 /// <summary>
 ///  Gets or sets the path to output the image 
 /// </summary>
 public string OutPath
 {
 get{return this.outPath;}
 set{this.outPath=value;}
  }
  #endregion

  #region  Start drawing watermark  DrawImage
  /// <summary>
 ///  Start drawing watermark 
 /// </summary>
  /// <example>
  /// <code>
  ///  Watermark wm = new Watermark();
  ///  wm.DrawedImagePath= Server.MapPath("") + "/upfile/" + "backlogo.gif";
  ///  wm.ModifyImagePath=path; 
  ///  wm.RightSpace=184;
  ///  wm.BottoamSpace=81;
  ///  wm.LucencyPercent=50;
  ///  wm.OutPath=Server.MapPath("") + "/upfile/" + fileName + "_new" + extension;
  ///  wm.DrawImage();
  ///  
  ///  // Save the watermarked picture , Delete the original picture  
  ///  mFileName=fileName + "_new" + extension;
  ///  if(File.Exists(path)) { File.Delete(path); } 
  /// </code>
  /// </example>
 public void DrawImage()
 {
 Image modifyImage=null;
 Image drawedImage=null;
 Graphics g=null;
 try { 
 modifyImage=Image.FromFile(this.ModifyImagePath);// Create a graphic object 
 drawedImage=Image.FromFile(this.DrawedImagePath);
 g=Graphics.FromImage(modifyImage);
 
 int x=modifyImage.Width-this.rightSpace;// Get that coordinate of the graph to be drawn 
 int y=modifyImage.Height-this.BottoamSpace;
 
 float[][] matrixItems ={// Set the color matrix 
   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, (float)this.LucencyPercent/100f, 0},
   new float[] {0, 0, 0, 0, 1}}; 

 ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
 ImageAttributes imgAttr=new ImageAttributes();
 imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
 
 g.DrawImage(// Drawing Shadow Image 
  drawedImage,
  new Rectangle(x,y,drawedImage.Width,drawedImage.Height),
  0,0,drawedImage.Width,drawedImage.Height,
  GraphicsUnit.Pixel,imgAttr);
 
 string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};// Save a file 
 FileInfo file=new FileInfo(this.ModifyImagePath);
 ImageFormat imageType=ImageFormat.Gif;
 switch(file.Extension.ToLower()) {
  case ".jpg": imageType=ImageFormat.Jpeg; break;
  case ".gif": imageType=ImageFormat.Gif; break;
  case ".png": imageType=ImageFormat.Png; break;
  case ".bmp": imageType=ImageFormat.Bmp; break;
  case ".tif": imageType=ImageFormat.Tiff; break;
  case ".wmf": imageType=ImageFormat.Wmf; break;
  case ".ico": imageType=ImageFormat.Icon; break;
  default: break;
 }
 MemoryStream ms=new MemoryStream();
 modifyImage.Save(ms,imageType);
 byte[] imgData=ms.ToArray();
 modifyImage.Dispose();
 drawedImage.Dispose();
 g.Dispose();
 FileStream fs=null;
 if(this.OutPath==null || this.OutPath=="") {
  File.Delete(this.ModifyImagePath);
  fs=new FileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write);
 } else {
  fs=new FileStream(this.OutPath,FileMode.Create,FileAccess.Write);
 }
 if(fs!=null) {
  fs.Write(imgData,0,imgData.Length);
  fs.Close();
 }
 } finally {
 try {
  drawedImage.Dispose();
  modifyImage.Dispose();
  g.Dispose();
 } catch{}
 }
 }
  #endregion
 }
}

Related articles: