It was more than 3 weeks since my last post, I thought to have blog post about image watermarking as last week I wanted that feature for a website requirement. Even I thought that is a big case, I could do it with couple of lines. Here see the code of lines for that.
First of all following namespaces are required for the code.
System.Drawing
System.Drawing.Drawing2D
System.Drawing.Imaging
Code:
string Imagefile = @”E:\projects\Watermarking\112.jpg”;//image path
Image img = Image.FromFile(Imagefile);// Create image object
Graphics g = Graphics.FromImage(img); //Create graphics object
Color WatermarkColor = Color.FromArgb(200, 84, 84, 84); //color with alpha
Font font = new Font(“Times New Roman”, 50,FontStyle.Italic);
Brush brush = new SolidBrush (WatermarkColor);
string WaterMarkString = “this is sample text”;
SizeF sz = g.MeasureString(WaterMarkString, font);
g.DrawString(WaterMarkString, font, brush,
new Point(X, Y));// Draw image with lable.
img.Save(“image.jpg”);// save image
Leave a Reply