SoFunction
Updated on 2025-03-04

C# Add a complete example of watermarking to the image

This article describes the method of adding watermarks to images by C#. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
/// <summary>
///ImgWater summary description/// </summary>
public class ImgWater
{
 public ImgWater()
 {
  //
  //TODO: Add constructor logic here  //
 }
 /// <summary>
 /// Picture watermark /// </summary>
 /// <param name="ImgFile">original image file address</param> /// <param name="WaterImg">Watermark image address</param> /// <param name="sImgPath">Watermark image saving address</param> /// <param name="Alpha">Watermark transparency settings</param> /// <param name="iScale">Scale of display of watermark images on the original image</param> /// <param name="intDistance">The margins of the watermark image on the original image are determined, and the right and bottom of the image are the prerequisite. When the set margin exceeds a certain size, the parameters will automatically fail</param> public bool zzsImgWater(
   string ImgFile
  , string WaterImg
  , string sImgPath
  , float Alpha
  , float iScale
  , int intDistance
  )
  {
  try
  {
   //Load pictures   FileStream fs = new FileStream(ImgFile, );
   BinaryReader br = new BinaryReader(fs);
   byte[] bytes = ((int));
   ();
   ();
   MemoryStream ms = new MemoryStream(bytes);
    imgPhoto = (ms);
   // imgPhoto = (ImgFile);
   int imgPhotoWidth = ;
   int imgPhotoHeight = ;
    imgWatermark = new Bitmap(WaterImg);
   int imgWatermarkWidth = ;
   int imgWatermarkHeight = ;
   //Calculate the size of the watermark image   decimal aScale = (iScale);
   decimal pScale = 0.05M;
   decimal MinScale = aScale - pScale;
   decimal MaxScale = aScale + pScale;
   int imgWatermarkWidthNew = imgWatermarkWidth;
   int imgWatermarkHeightNew = imgWatermarkHeight;
   if (imgPhotoWidth &gt;= imgWatermarkWidth &amp;&amp; imgPhotoHeight &gt;= imgWatermarkHeight &amp;&amp; imgPhotoWidth &gt;= imgPhotoHeight)
    if (imgWatermarkWidth &gt; imgWatermarkHeight)
     if ((MinScale &lt;= (((imgWatermarkWidth) / (imgPhotoWidth)), 7)) &amp;&amp; ((((imgWatermarkWidth) / (imgPhotoWidth)), 7) &lt;= MaxScale))
      {
     }
     else
      {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale &lt;= (((imgWatermarkHeight) / (imgPhotoHeight)), 7)) &amp;&amp; ((((imgWatermarkHeight) / (imgPhotoHeight)), 7) &lt;= MaxScale))
     {
     }
     else
      {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgWatermarkWidth &gt;= imgWatermarkHeight)
    {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &lt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &gt;= imgPhotoHeight)
    {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth &lt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &gt;= imgPhotoHeight)
    {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgPhotoWidth &gt;= imgWatermarkWidth &amp;&amp; imgPhotoHeight &gt;= imgWatermarkHeight &amp;&amp; imgPhotoWidth &lt;= imgPhotoHeight)
    if (imgWatermarkWidth &gt; imgWatermarkHeight)
     if ((MinScale &lt;= (((imgWatermarkWidth) / (imgPhotoWidth)), 7)) &amp;&amp; ((((imgWatermarkWidth) / (imgPhotoWidth)), 7) &lt;= MaxScale))
      {
     }
     else
      {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale &lt;= (((imgWatermarkHeight) / (imgPhotoHeight)), 7)) &amp;&amp; ((((imgWatermarkHeight) / (imgPhotoHeight)), 7) &lt;= MaxScale))
      {
     }
     else
      {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgWatermarkWidth &lt;= imgWatermarkHeight)
    {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &lt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &lt;= imgPhotoHeight)
    {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth &lt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &lt;= imgPhotoHeight)
    {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   //Draw the original picture   Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, .Format24bppRgb);
   (72, 72);
   Graphics gbmPhoto = (bmPhoto);
    = .;
    = .;
   ();
   (
     imgPhoto
    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
    , 0
    , 0
    , imgPhotoWidth
    , imgPhotoHeight
    , 
    );
   Bitmap bmWatermark = new Bitmap(bmPhoto);
   (, );
   Graphics gWatermark = (bmWatermark);
   //Specify high-quality display of watermark image quality    = .;
    = .;
    ImageAttributes imageAttributes = new ImageAttributes();
   //Set two colors to achieve the synthesis effect   ColorMap colorMap = new ColorMap();
    = (255, 0, 255, 0);
    = (0, 0, 0, 0);
   ColorMap[] remapTable = { colorMap };
   (remapTable, );
     //Use matrix to set the transparency of the watermark image   float[][] colorMatrixElements = { 
    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, Alpha, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
   };
    wmColorMatrix = new (colorMatrixElements);
   (wmColorMatrix, , );
   //Determine the watermark margin   int xPos = imgPhotoWidth - imgWatermarkWidthNew;
   int yPos = imgPhotoHeight - imgWatermarkHeightNew;
   int xPosOfWm = 0;
   int yPosOfWm = 0;
   if (xPos &gt; intDistance)
    xPosOfWm = xPos - intDistance;
   else
    xPosOfWm = xPos;
   if (yPos &gt; intDistance)
    yPosOfWm = yPos - intDistance;
   else
    yPosOfWm = yPos;
   (
     imgWatermark
    , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
    , 0
    , 0
    , imgWatermarkWidth
    , imgWatermarkHeight
    , 
    , imageAttributes
    );
   imgPhoto = bmWatermark;
   //Save pictures in jpg format   (sImgPath, );
   //Destroy the object   ();
   ();
   ();
   ();
   return true;
  }
  catch
  {
   return false ;
  }
 }
 /**//// <summary>
  /// Text watermark
  /// </summary>
  /// <param name="ImgFile">original image file address</param>
  /// <param name="TextFont">Watermark text</param>
  /// <param name="sImgPath">Text watermark image saving address</param>
  /// <param name="hScale">Height position</param>
  /// <param name="widthFont">The proportion of width of text blocks in pictures</param>
  /// <param name="Alpha">Text transparency The range of values ​​is 0 to 255</param>
  public bool zzsTextWater(
    string ImgFile
   , string TextFont
   , string sImgPath
   float hScale
   float widthFont
   , int Alpha
   )
   {
   try
   {
    FileStream fs = new FileStream(ImgFile, );
    BinaryReader br = new BinaryReader(fs);
    byte[] bytes = ((int));
    ();
    ();
    MemoryStream ms = new MemoryStream(bytes);
     imgPhoto = (ms);
    // imgPhoto = (ImgFile);
    int imgPhotoWidth = ;
    int imgPhotoHeight = ;
    Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, .Format24bppRgb);
    (72, 72);
    Graphics gbmPhoto = (bmPhoto);
     = .;
     = .;
    (
      imgPhoto
     , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
     , 0
     , 0
     , imgPhotoWidth
     , imgPhotoHeight
     ,
     );
    //Create an array of font sizes and loop to find the watermark font suitable for the picture
    //int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
    int[] sizes = new int[] { 28, 26, 24, 20, 16, 14, 12 };
     crFont = null;
     crSize = new SizeF();
    for (int i = 0; i < 7; i++)
    {
     crFont = new Font("Microsoft Yahei", sizes[i], );
     crSize = (TextFont, crFont);
     if ((ushort) < (ushort)imgPhotoWidth * widthFont)
      break;
    }
    //Set the location of the watermark font
    //int yPixlesFromBottom = (int)(imgPhotoHeight * hScale);
    //float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - ( / 2));
    //float xCenterOfImg = (imgPhotoWidth * 1 / 2);
    float yPosFromBottom = imgPhotoHeight *0.85f;
    float xCenterOfImg = imgPhotoWidth * 0.8f;
    //if (xCenterOfImg<)
    // xCenterOfImg = (imgPhotoWidth * (1 - ()/ imgPhotoWidth));
     StrFormat = new ();
     = ;
    //
     semiTransBrush2 = new ((Alpha, 0, 0, 0));
    (
      TextFont
     , crFont
     , semiTransBrush2
     , new (xCenterOfImg + 1, yPosFromBottom + 1)
     , StrFormat
     );
     semiTransBrush = new ((Alpha, 255, 255, 255));
    //(semiTransBrush2, new RectangleF(new PointF(xCenterOfImg - / 2, yPosFromBottom - 4), crSize));
    (
      TextFont
     , crFont
     , semiTransBrush
     , new (xCenterOfImg, yPosFromBottom)
     , StrFormat
     );
    (sImgPath, );
    ();
    ();
    ();
    return true;
   }
   catch
   {
    return false;
   }
  }
  /**//// &lt;summary&gt;
 /// Watermark of text and logo pictures /// &lt;/summary&gt;
 /// <param name="ImgFile">original image file address</param> /// <param name="WaterImg">Watermark image address</param> /// <param name="TextFont">Watermark text information</param> /// <param name="sImgPath">Save address after survival watermark picture</param> /// <param name="ImgAlpha">Transparency of watermark pictures</param> /// <param name="imgiScale">Scale of display of watermark images on the original image</param> /// <param name="intimgDistance">The margins of the watermark image on the original image are determined, and the right and bottom of the image are the prerequisite. When the set margin exceeds a certain size, the parameters will automatically fail</param> /// <param name="textScale">The height position of the watermark text, start from the bottom of the picture, 0-1</param> /// <param name="textwidthFont">The width ratio of text blocks in the picture 0-1</param> /// <param name="textAlpha">Text transparency The range of values ​​is 0 to 255</param> public void zzsImgTextWater(
   string ImgFile
  , string WaterImg
  , string TextFont
  , string sImgPath
  , float ImgAlpha
  , float imgiScale
  , int intimgDistance
  , float texthScale
  , float textwidthFont
  , int textAlpha
  )
  {
  try
  {
   FileStream fs = new FileStream(ImgFile, );
   BinaryReader br = new BinaryReader(fs);
   byte[] bytes = ((int));
   ();
   ();
   MemoryStream ms = new MemoryStream(bytes);
    imgPhoto = (ms);
   // imgPhoto = (ImgFile);
   int imgPhotoWidth = ;
   int imgPhotoHeight = ;
   Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, .Format24bppRgb);
   (72, 72);
   Graphics gbmPhoto = (bmPhoto);
    = .;
    = .;
   (
     imgPhoto
    , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
    , 0
    , 0
    , imgPhotoWidth
    , imgPhotoHeight
    , 
    );
   //Create an array of font sizes and loop to find the watermark font suitable for the picture   int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
    crFont = null;
    crSize = new SizeF();
   for (int i = 0; i &lt; 43; i++)
   {
    crFont = new Font("arial", sizes[i], );
    crSize = (TextFont, crFont);
    if ((ushort) &lt; (ushort)imgPhotoWidth * textwidthFont)
     break;
   }
   //Set the location of the watermark font   int yPixlesFromBottom = (int)(imgPhotoHeight * texthScale);
   float yPosFromBottom = ((imgPhotoHeight - yPixlesFromBottom) - ( / 2));
   float xCenterOfImg = (imgPhotoWidth * 1 / 2);
    StrFormat = new ();
    = ;
   //
    semiTransBrush2 = new ((textAlpha, 0, 0, 0));
   (
     TextFont
    , crFont
    , semiTransBrush2
    , new (xCenterOfImg + 1, yPosFromBottom + 1)
    , StrFormat
    );
    semiTransBrush = new ((textAlpha, 255, 255, 255));
   (
     TextFont
    , crFont
    , semiTransBrush
    , new (xCenterOfImg, yPosFromBottom)
    , StrFormat
    );
    imgWatermark = new Bitmap(WaterImg);
   int imgWatermarkWidth = ;
   int imgWatermarkHeight = ;
   //Calculate the size of the watermark image   decimal aScale = (imgiScale);
   decimal pScale = 0.05M;
   decimal MinScale = aScale - pScale;
   decimal MaxScale = aScale + pScale;
   int imgWatermarkWidthNew = imgWatermarkWidth;
   int imgWatermarkHeightNew = imgWatermarkHeight;
   if (imgPhotoWidth &gt;= imgWatermarkWidth &amp;&amp; imgPhotoHeight &gt;= imgWatermarkHeight &amp;&amp; imgPhotoWidth &gt;= imgPhotoHeight)
    if (imgWatermarkWidth &gt; imgWatermarkHeight)
     if ((MinScale &lt;= (((imgWatermarkWidth) / (imgPhotoWidth)), 7)) &amp;&amp; ((((imgWatermarkWidth) / (imgPhotoWidth)), 7) &lt;= MaxScale))
     {
     }
     else
     {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale &lt;= (((imgWatermarkHeight) / (imgPhotoHeight)), 7)) &amp;&amp; ((((imgWatermarkHeight) / (imgPhotoHeight)), 7) &lt;= MaxScale))
     {
     }
     else
     {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgWatermarkWidth &gt;= imgWatermarkHeight)
   {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &lt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &gt;= imgPhotoHeight)
   {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth &lt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &gt;= imgPhotoHeight)
   {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgPhotoWidth &gt;= imgWatermarkWidth &amp;&amp; imgPhotoHeight &gt;= imgWatermarkHeight &amp;&amp; imgPhotoWidth &lt;= imgPhotoHeight)
    if (imgWatermarkWidth &gt; imgWatermarkHeight)
     if ((MinScale &lt;= (((imgWatermarkWidth) / (imgPhotoWidth)), 7)) &amp;&amp; ((((imgWatermarkWidth) / (imgPhotoWidth)), 7) &lt;= MaxScale))
     {
     }
     else
     {
      imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
      imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
     }
    else
     if ((MinScale &lt;= (((imgWatermarkHeight) / (imgPhotoHeight)), 7)) &amp;&amp; ((((imgWatermarkHeight) / (imgPhotoHeight)), 7) &lt;= MaxScale))
     {
     }
     else
     {
      imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
      imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
     }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgWatermarkWidth &lt;= imgWatermarkHeight)
   {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   if (imgWatermarkWidth &gt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &lt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &lt;= imgPhotoHeight)
   {
    imgWatermarkWidthNew = Convert.ToInt32(imgPhotoWidth * aScale);
    imgWatermarkHeightNew = Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
   }
   if (imgWatermarkWidth &lt;= imgPhotoWidth &amp;&amp; imgWatermarkHeight &gt;= imgPhotoHeight &amp;&amp; imgPhotoWidth &lt;= imgPhotoHeight)
   {
    imgWatermarkHeightNew = Convert.ToInt32(imgPhotoHeight * aScale);
    imgWatermarkWidthNew = Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
   }
   //Draw the original picture   Bitmap bmWatermark = new Bitmap(bmPhoto);
   (, );
   Graphics gWatermark = (bmWatermark);
   //Specify high-quality display of watermark image quality    = .;
    = .;
    imageAttributes = new ();
   //Set two colors to achieve the synthesis effect    colorMap = new ();
    = (255, 0, 255, 0);
    = (0, 0, 0, 0);
   [] remapTable = { colorMap };
   (remapTable, );
   //Use matrix to set the transparency of the watermark image   float[][] colorMatrixElements = { 
    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, ImgAlpha, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
   };
    wmColorMatrix = new (colorMatrixElements);
   (wmColorMatrix, , );
   //Determine the watermark margin   int xPos = imgPhotoWidth - imgWatermarkWidthNew;
   int yPos = imgPhotoHeight - imgWatermarkHeightNew;
   int xPosOfWm = 0;
   int yPosOfWm = 0;
   if (xPos &gt; intimgDistance)
    xPosOfWm = xPos - intimgDistance;
   else
    xPosOfWm = xPos;
   if (yPos &gt; intimgDistance)
    yPosOfWm = yPos - intimgDistance;
   else
    yPosOfWm = yPos;
   (
     imgWatermark
    , new Rectangle(xPosOfWm, yPosOfWm, imgWatermarkWidthNew, imgWatermarkHeightNew)
    , 0
    , 0
    , imgWatermarkWidth
    , imgWatermarkHeight
    , 
    , imageAttributes
    );
   imgPhoto = bmWatermark;
   //Save pictures in jpg format   (sImgPath, );
   //Destroy the object   ();
   ();
   ();
   ();
   ();
  }
  catch 
  {
  }
 }
 /**//// <summary>
  /// Thumbnail
  /// </summary>
  /// <param name="ImgFile">original image file address</param>
  /// <param name="sImgPath">Thumbnail save address</param>
  /// <param name="ResizeWidth">Thumbnail width</param>
  /// <param name="ResizeHeight">Thumbnail height</param>
  /// <param name="BgColor">Thumbnail background color, note that the background color can only specify the values ​​in KnownColor, such as blue, red, green, etc.</param>
  public bool zzsResizeImg( string ImgFile , string sImgPath , int ResizeWidth , int ResizeHeight , string BgColor )
   {
   try
   {
    FileStream fs = new FileStream(ImgFile, );
    BinaryReader br = new BinaryReader(fs);
    byte[] bytes = ((int));
    ();
    ();
    MemoryStream ms = new MemoryStream(bytes);
     imgPhoto = (ms);
    // imgPhoto = (ImgFile);
    int imgPhotoWidth = ;
    int imgPhotoHeight = ;
    int startX = 0;
    int StartY = 0;
    int NewWidth = 0;
    int NewHeight = 0;
    if (imgPhotoWidth >= ResizeWidth && imgPhotoHeight >= ResizeHeight)
    {
     NewWidth = ResizeWidth;
     NewHeight = Convert.ToInt32(imgPhotoHeight * ((ResizeWidth) / (imgPhotoWidth), 10));
     startX = 0;
     StartY = (ResizeHeight - NewHeight) / 2;
    }
    if (ResizeWidth > imgPhotoWidth && ResizeHeight < imgPhotoHeight)
    {
     NewHeight = ResizeHeight;
     NewWidth = Convert.ToInt32(imgPhotoWidth * ((ResizeHeight) / (imgPhotoHeight), 10));
     startX = (ResizeWidth - NewWidth) / 2;
     StartY = 0;
    }
    if (ResizeWidth < imgPhotoWidth && ResizeHeight > imgPhotoHeight)
    {
     NewWidth = ResizeWidth;
     NewHeight = Convert.ToInt32(imgPhotoHeight * ((ResizeWidth) / (imgPhotoWidth), 10));
     startX = 0;
     StartY = (ResizeHeight - NewHeight) / 2;
    }
    if (imgPhotoWidth < ResizeWidth && imgPhotoHeight < ResizeHeight)
    {
     NewWidth = imgPhotoWidth;
     NewHeight = imgPhotoHeight;
     startX = (ResizeWidth - imgPhotoWidth) / 2;
     StartY = (ResizeHeight - imgPhotoHeight) / 2;
    }
    //Calculate the size of the zoom picture
    Bitmap bmPhoto = new Bitmap(ResizeWidth, ResizeHeight, .Format24bppRgb);
    (72, 72);
    Graphics gbmPhoto = (bmPhoto);
    ((BgColor));
     = .;
     = .;
    (
      imgPhoto
     , new Rectangle(startX, StartY, NewWidth, NewHeight)
     , new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight)
     ,
     );
    (sImgPath, );
    ();
    ();
    ();
    ();
    return true;
   }
   catch
   {
    return false;
   }
  }
  /**//// &lt;summary&gt;
 /// Picture cut /// &lt;/summary&gt;
 /// <param name="ImgFile">original image file address</param> /// <param name="sImgPath">Thumbnail save address</param> /// <param name="PointX">Cut the starting point X coordinate</param> /// <param name="PointY">Cut the starting point Y coordinate</param> /// <param name="CutWidth">CutWidth</param> /// <param name="CutHeight">Shear Height</param> public bool zzsCutImg(string ImgFile, string sImgPath, int PointX, int PointY, int CutWidth, int CutHeight)
 {
  FileStream fs = new FileStream(ImgFile, );
  BinaryReader br = new BinaryReader(fs);
  try
  {
   byte[] bytes = ((int));
   ();
   ();
   MemoryStream ms = new MemoryStream(bytes);
    imgPhoto = (ms);
   // imgPhoto = (ImgFile);
   //You can only use filestream here. If you use it, you will report more errors than processes accessing files and will lock the file   Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, .Format24bppRgb);
   (72, 72);
   Graphics gbmPhoto = (bmPhoto);
    = .;
    = .;
   (
     imgPhoto
    , new Rectangle(0, 0, CutWidth, CutHeight)
    , new Rectangle(PointX, PointY, CutHeight, CutHeight)
    , 
    );
   (sImgPath, );
   ();
   ();
   ();
   ();
   return true;
  }
  catch
  {
   return false;
  }
  finally
  {
  }
 }
}

I hope this article will be helpful to everyone's C# programming.