SoFunction
Updated on 2025-03-06

C# Code summary for adding text to images


using System;
using ;
using ;
using ;
using .Drawing2D;
using ;
namespace Imag_writer
{
/// <summary>
/// Type of watermark
/// </summary>
public enum WaterMarkType
{
/// <summary>
/// Text watermark
/// </summary>
TextMark,
/// <summary>
/// Picture watermark
/// </summary>
//ImageMark // Only text watermarks can be added for the time being
};
/// <summary>
/// The location of the watermark
/// </summary>
public enum WaterMarkPosition
{
/// <summary>
/// upper left corner
/// </summary>
WMP_Left_Top,
/// <summary>
/// Lower left corner
/// </summary>
WMP_Left_Bottom,
/// <summary>
/// upper right corner
/// </summary>
WMP_Right_Top,
/// <summary>
/// Lower right corner
/// </summary>
WMP_Right_Bottom
};
/// <summary>
/// Classes for processing images (including adding watermarks and generating thumbnails)
/// </summary>
public class ImageWaterMark
{
public ImageWaterMark()
{
  //
// TODO: Add constructor logic here
  //
}
#region Add watermark to the picture
/// <summary>
/// Add watermark (subject to image watermark and text watermark)
/// </summary>
/// <param name="oldpath">Original image absolute address</param>
/// <param name="newpath">Absolute address for new image placement</param>
/// <param name="wmtType">Type of watermark to be added</param>
/// <param name="sWaterMarkContent">Watermark content, if you add a text watermark, this is the text to be added;
/// To add an image watermark, this is the path to the image</param>
public void addWaterMark(string oldpath, string newpath,
  WaterMarkType wmtType, string sWaterMarkContent)
{
  try
  {
Image image = (oldpath);
Bitmap b = new Bitmap(, ,
  PixelFormat.Format24bppRgb);
Graphics g = (b);
();
= ;
= ;
(image, 0, 0, , );
switch (wmtType)
{
  case :
// Text watermark
(g, sWaterMarkContent, "WM_BOTTOM_RIGHT",
  , );
break;
}
(newpath);
();
();
  }
  catch
  {
if((oldpath))
{
  (oldpath);
}
  }
  finally
  {
if((oldpath))
{
  (oldpath);
}
  }
}
/// <summary>
/// Add watermark text
/// </summary>
/// <param name="picture">imge object</param>
/// <param name="_watermarkText">Watermark text content</param>
/// <param name="_watermarkPosition">Watermark Position</param>
/// <param name="_width">Width of the watermarked image</param>
/// <param name="_height">High of watermarked pictures</param>
private void addWatermarkText(Graphics picture, string _watermarkText,
  string _watermarkPosition, int _width, int _height)
{
// Determine the font size of the watermark text
  int[] sizes = new int[]{32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4};
  Font crFont = null;
  SizeF crSize = new SizeF();
  for (int i = 0;i < ; i++)
  {
crFont = new Font("Arial Black", sizes[i], );
crSize = (_watermarkText, crFont);
if((ushort) < (ushort)_width)
{
  break;
}
  }
// Generate a watermark image (write text into the image)
  Bitmap floatBmp = new Bitmap((int) + 3,
  (int) + 3, PixelFormat.Format32bppArgb);
  Graphics fg=(floatBmp);
  PointF pt=new PointF(0,0);
// Draw shadow text
  Brush TransparentBrush0 = new SolidBrush((255, ));
  Brush TransparentBrush1 = new SolidBrush((255, ));
  (_watermarkText,crFont,TransparentBrush0, , + 1);
  (_watermarkText,crFont,TransparentBrush0, + 1, );
  (_watermarkText,crFont,TransparentBrush1, + 1, + 1);
  (_watermarkText,crFont,TransparentBrush1, , + 2);
  (_watermarkText,crFont,TransparentBrush1, + 2, );
  ();
  ();
// Draw text
  =.;
  (_watermarkText,
crFont, new SolidBrush(),
, , );
// Save the operation just now
  ();
  ();
  // ("d:\\WebSite\\DIGITALKM\\");
// Add the watermark image to the original image
  (
picture,
new Bitmap(floatBmp),
"WM_BOTTOM_RIGHT",
_width,
_height);
}
/// <summary>
/// Add watermark picture
/// </summary>
/// <param name="picture">imge object</param>
/// <param name="iTheImage">Image object (using this image as a watermark)</param>
/// <param name="_watermarkPosition">Watermark Position</param>
/// <param name="_width">Width of the watermarked image</param>
/// <param name="_height">High of watermarked pictures</param>
private void addWatermarkImage( Graphics picture,Image iTheImage,
  string _watermarkPosition,int _width,int _height)
{
  Image watermark = new Bitmap(iTheImage);
  ImageAttributes imageAttributes = new ImageAttributes();
  ColorMap colorMap = new ColorMap();
  = (255, 0, 255, 0);
  = (0, 0, 0, 0);
  ColorMap[] remapTable = {colorMap};
  (remapTable, );
  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, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  };
  ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  (colorMatrix, , );
  int xpos = 0;
  int ypos = 0;
  int WatermarkWidth = 0;
  int WatermarkHeight = 0;
  double bl = 1d;
//Calculate the ratio of watermark pictures
//Compare 1/4 of the background width
  if ((_width > * 4) && (_height > * 4))
  {
bl = 1;
  }
  else if ((_width > * 4) && (_height< * 4))
  {
bl = (_height / 4) / ();
  }
  else if ((_width < * 4) && (_height > * 4))
  {
bl = (_width / 4) / ();
  }
  else
  {
if ((_width * ) > (_height * ))
{
  bl = (_height / 4) / ();
}
else
{
  bl = (_width / 4) / ();
}
  }
  WatermarkWidth = Convert.ToInt32( * bl);
  WatermarkHeight = Convert.ToInt32( * bl);
  switch (_watermarkPosition)
  {
case "WM_TOP_LEFT":
  xpos = 10;
  ypos = 10;
  break;
case "WM_TOP_RIGHT":
  xpos = _width - WatermarkWidth - 10;
  ypos = 10;
  break;
case "WM_BOTTOM_RIGHT":
  xpos = _width - WatermarkWidth - 10;
  ypos = _height -WatermarkHeight - 10;
  break;
case "WM_BOTTOM_LEFT":
  xpos = 10;
  ypos = _height - WatermarkHeight - 10;
  break;
  }
  (
watermark,
new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight),
0,
0,
,
,
,
imageAttributes);
  ();
  ();
}
/// <summary>
/// Add watermark picture
/// </summary>
/// <param name="picture">imge object</param>
/// <param name="WaterMarkPicPath">Address of watermark picture</param>
/// <param name="_watermarkPosition">Watermark Position</param>
/// <param name="_width">Width of the watermarked image</param>
/// <param name="_height">High of watermarked pictures</param>
private void addWatermarkImage( Graphics picture,string WaterMarkPicPath,
  string _watermarkPosition,int _width,int _height)
{
  Image watermark = new Bitmap(WaterMarkPicPath);
  (picture, watermark, _watermarkPosition, _width,
_height);
}
#endregion
#region Generate thumbnails
/// <summary>
/// Save the picture
/// </summary>
/// <param name="image">Image object</param>
/// <param name="savePath">SavePath</param>
/// <param name="ici">Designed format codec parameters</param>
private void SaveImage(Image image, string savePath, ImageCodecInfo ici)
{
//Set the EncoderParameters object of the original image object
  EncoderParameters parameters = new EncoderParameters(1);
  [0] = new EncoderParameter(
, ((long) 90));
  (savePath, ici, parameters);
  ();
}
/// <summary>
/// Get all relevant information about the image encoding codec
/// </summary>
/// <param name="mimeType">Stands of type of multipurpose Internet Mail Extension Protocol (MIME) that contains encoding and decoder</param>
/// <returns>Return all relevant information about the image encoding codec</returns>
private ImageCodecInfo GetCodecInfo(string mimeType)
{
  ImageCodecInfo[] CodecInfo = ();
  foreach(ImageCodecInfo ici in CodecInfo)
  {
if( == mimeType)
  return ici;
  }
  return null;
}
/// <summary>
/// Generate thumbnails
/// </summary>
/// <param name="sourceImagePath">Original image path (relative path)</param>
/// <param name="thumbnailImagePath">The generated thumbnail path, if empty, save as the original image path (relative path)</param>
/// <param name="thumbnailImageWidth">Width of thumbnail (height is automatically generated according to the ratio of the source image)</param>
public void ToThumbnailImages(
  string SourceImagePath,
  string ThumbnailImagePath,
  int ThumbnailImageWidth)
{
  Hashtable htmimes = new Hashtable();
  htmimes[".jpeg"] = "image/jpeg";
  htmimes[".jpg"] = "image/jpeg";
  htmimes[".png"] = "image/png";
  htmimes[".tif"] = "image/tiff";
  htmimes[".tiff"] = "image/tiff";
  htmimes[".bmp"] = "image/bmp";
  htmimes[".gif"] = "image/gif";
// Get the suffix of the original picture
  string sExt = (
(".")).ToLower();
//Create Image object from the original image
  Image image = (SourceImagePath);
  int num = ((ThumbnailImageWidth / 4) * 3);
  int width = ;
  int height = ;
//Calculate the proportion of the picture
  if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
  {
num = ((height * ThumbnailImageWidth) / width);
  }
  else
  {
ThumbnailImageWidth = ((width * num) / height);
  }
  if ((ThumbnailImageWidth < 1) || (num < 1))
  {
return;
  }
//Initialize a new instance of the Bitmap class with the specified size and format
  Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num,
PixelFormat.Format32bppArgb);
//Create a new Graphics object from the specified Image object
  Graphics graphics = (bitmap);
//Clear the entire drawing surface and fill it with transparent background color
  ();
  = ;
  = ;
//Draw the original picture object at the specified position and according to the specified size
  (image, new Rectangle(0, 0, ThumbnailImageWidth, num));
  ();
  try
  {
//Save this original image in the specified format and use the specified codec parameters to the specified file
SaveImage(bitmap, ThumbnailImagePath,
  GetCodecInfo((string)htmimes[sExt]));
  }
  catch( e)
  {
throw e;
  }
}
#endregion
}
}