While working on one of my friend’s blog, I got a chance to write a code snippet which helps to add water mark for all images automatically. And I created a simple library do this. I added few extra options, it may not work properly, I didn’t tested it
namespace dotnetthoughts.net
{
using System.Drawing;
/// <summary>
/// </summary>
public class WaterMarker
{
/// <summary>
/// Sets / Gets color of the Watermark text.
/// </summary>
public Color Color { get; set; }
/// <summary>
/// Sets or Gets Font of Watermark Text.
/// </summary>
public Font Font { get; set; }
/// <summary>
/// Sets or Gets Alpha value of the Color.
/// </summary>
/// <remarks>
/// It should be between 0 and 255.
/// </remarks>
public int Alpha { get; set; }
/// <summary>
/// Sets or Gets the Watermark Text
/// </summary>
public string WatermarkText { get; set; }
/// <summary>
/// Sets or Gets the alignment of Watermark text
/// </summary>
public ContentAlignment TextAlignment { get; set; }
/// <summary>
/// Creates an instance of Watermarker class.
/// </summary>
public WaterMarker()
{
Color = Color.White;
Alpha = 200;
Font = new Font(FontFamily.GenericSerif, 20);
WatermarkText = "dotnetthoughts.net";
TextAlignment = ContentAlignment.BottomCenter;
}
/// <summary>
/// Adds the watermark text to the given image.
/// </summary>
/// <param name="image">Image to apply watermark</param>
/// <param name="waterMarkText">Watermark Text</param>
/// <returns>Image</returns>
public Image Apply(Image image, string waterMarkText)
{
WatermarkText = waterMarkText;
using (Graphics graphics = Graphics.FromImage(image))
{
var size =
graphics.MeasureString(WatermarkText, Font);
var brush =
new SolidBrush(Color.FromArgb(Alpha, Color));
graphics.DrawString
(WatermarkText, Font, brush,
GetTextPosition(image, size));
}
return image;
}
/// <summary>
/// Adds the watermark text to the given image.
/// </summary>
/// <param name="image">Image to apply watermark</param>
/// <returns>Image</returns>
public Image Apply(Image image)
{
return Apply(image, WatermarkText);
}
private PointF GetTextPosition(Image image, SizeF size)
{
PointF point = default(PointF);
switch (TextAlignment)
{
case ContentAlignment.BottomCenter:
point = new PointF((image.Width - size.Width) / 2,
(image.Height - size.Height));
break;
case ContentAlignment.BottomLeft:
point = new PointF(0, (image.Height - size.Height));
break;
case ContentAlignment.BottomRight:
point = new PointF((image.Width - size.Width),
(image.Height - size.Height));
break;
case ContentAlignment.MiddleCenter:
point = new PointF((image.Width - size.Width) / 2,
(image.Height - size.Height) / 2);
break;
case ContentAlignment.MiddleLeft:
point = new PointF(0, (image.Height - size.Height) / 2);
break;
case ContentAlignment.MiddleRight:
point = new PointF((image.Width - size.Width),
(image.Height - size.Height) / 2);
break;
case ContentAlignment.TopCenter:
point = new PointF((image.Width - size.Width) / 2, 0);
break;
case ContentAlignment.TopLeft:
point = new PointF(0, 0);
break;
case ContentAlignment.TopRight:
point = new PointF((image.Width - size.Width), 0);
break;
}
return point;
}
}
}
Happy Coding