SoFunction
Updated on 2025-03-06

Example code for adding watermark to c# image


using System;
using ;
using .Drawing2D;
using ;
using ;
namespace ImageDrawing
{
 /// <summary>
/// Image modification category is mainly used to protect image copyright
 /// </summary>
 public class ImageModification
 {
  #region "member fields"
  private string modifyImagePath=null;
  private string drawedImagePath=null;
  private int rightSpace;
  private int bottoamSpace;
  private int lucencyPercent=70;
  private string outPath=null;
  #endregion
  public ImageModification()
  {
  }
  #region "propertys"
  /// <summary>
/// Get or set the image path to modify
  /// </summary>
  public string ModifyImagePath
  {
   get{return ;}
   set{=value;}
  }
  /// <summary>
/// Get or set the image path (watermark image)
  /// </summary>
  public string DrawedImagePath
  {
   get{return ;}
   set{=value;}
  }
  /// <summary>
/// Get or set the right margin of the watermark in the modified picture
  /// </summary>
  public int RightSpace
  {
   get{return ;}
   set{=value;}
  }
//Get or set the height of the watermark at the bottom of the middle of the image
  public int BottoamSpace
  {
   get{return ;}
   set{=value;}
  }
  /// <summary>
/// Get or set the transparency of the watermark to be drawn, pay attention to the percentage of the original image transparency
  /// </summary>
  public int LucencyPercent
  {
   get{return ;}
   set
   {
    if(value>=0&&value<=100)
     =value;
   }
  }
  /// <summary>
/// Get or set the path to output the image
  /// </summary>
  public string OutPath
  {
   get{return ;}
   set{=value;}
  }
  #endregion
  #region "methods"
  /// <summary>
/// Start drawing watermarks
  /// </summary>
  public void DrawImage()
  {
   Image modifyImage=null;
   Image drawedImage=null;
   Graphics g=null;
   try
   {   
//Create a graphic object
    modifyImage=();
    drawedImage=();
    g=(modifyImage);
//Get the coordinates of the graphic to be drawn
    int x=;
    int y=;
//Set the color matrix
    float[][] matrixItems ={
             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)/100f, 0},
             new float[] {0, 0, 0, 0, 1}};
    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
    ImageAttributes imgAttr=new ImageAttributes();
    (colorMatrix,,);
//Draw shadow image
    (
     drawedImage,
     new Rectangle(x,y,,),
     0,0,,,
     ,imgAttr);
//Save the file
    string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};
    FileInfo file=new FileInfo();
    ImageFormat imageType=;
    switch(())
    {
     case ".jpg":
      imageType=;
      break;
     case ".gif":
      imageType=;
      break;
     case ".png":
      imageType=;
      break;
     case ".bmp":
      imageType=;
      break;
     case ".tif":
      imageType=;
      break;
     case ".wmf":
      imageType=;
      break;
     case ".ico":
      imageType=;
      break;
     default:
      break;
    }
    MemoryStream ms=new MemoryStream();
    (ms,imageType);
    byte[] imgData=();
    ();
    ();
    ();
    FileStream fs=null;
    if(==null || =="")
    {
     ();
     fs=new FileStream(,,);
    }
    else
    {
     fs=new FileStream(,,);
    }
    if(fs!=null)
    {
     (imgData,0,);
     ();
    }
   }
   finally
   {
    try
    {
     ();
     ();
     ();
    }
    catch{;}
   }
  }
  #endregion
 }
}