How To Add Watermark Text To Images in C#
By FoxLearn 11/15/2024 7:44:28 AM 7.13K
Watermark is very useful when you are uploading images to website. It protects against copying images from the source website. It's a type of overlay added to an image, typically to indicate ownership, copyright, or to provide additional information about the image. It's often semi-transparent and placed in a corner or another unobtrusive location on the image.
Watermark text helps deter unauthorized use of the image while still allowing viewers to see the content. It can contain various information such as the creator's name, a copyright symbol, a website URL, or any other relevant details. Adding watermark text is a common practice in photography, graphic design, and other creative fields to protect intellectual property rights.
How To Add Watermark Text To Images in C#
You can add a watermark text to images in C# using the .NET framework. One way to achieve this is by utilizing the System.Drawing
namespace.
Open Visual Studio, then create a new Windows Forms Application.
Drag and drop the PictureBox, Button controls from the Visual Studio toolbox into your form designer, then design your form with controls to load an image, and a button to apply the watermark.
For simplicity, you can use a PictureBox
to display the image, set text from code behide or TextBox
for entering text, and a Button
to trigger the watermark application.
Add a click event handler to the Convert button allows you to open a image, then add watermark to image with text in c#.
private void btnConvert_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg|PNG|*.png" }) { if (ofd.ShowDialog() == DialogResult.OK) { Image img = Image.FromFile(ofd.FileName); pictureBox1.Image = img; pictureBox2.Image = CreateWatermarkImage(img, "foxlearn.com", 50); } } }
We'll open an image, then assign it to PictureBox1. Next we will create a watermark on the image just opened with text and assign it to PictureBox2.
And don't forget create the CreateWatermarkImage method as shown below. This is a simple way to add text to image in c#.
//add a text watermark in C# public Image CreateWatermarkImage(Image img, string text, int size) { Bitmap bmp = new Bitmap(img); //choose font for text Font font = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel); //c# add transparent watermark image Color color = Color.LightGray; //location of the watermark text in the source image Point point = new Point(bmp.Width / 2, bmp.Height / 2); SolidBrush brush = new SolidBrush(color); //c# add text to image Graphics g = Graphics.FromImage(bmp); g.DrawString(text, font, brush, point); g.Dispose(); return bmp; }
If you want to write watermark to image file, you can modify your code as shown below.
//c# watermark text on image public void CreateWatermarkImage(string fileName, string text) { Image img = Image.FromFile(fileName); Bitmap bmp = new Bitmap(img); // choose font for text Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel); //choose color and transparency Color color = Color.FromArgb(100, 211, 211, 211); //location of the watermark text in the parent image Point point = new Point(bmp.Width / 2, bmp.Height / 2); SolidBrush brush = new SolidBrush(color); //draw text on image Graphics g = Graphics.FromImage(bmp); g.DrawString(text, font, brush, point); g.Dispose(); img.Dispose(); bmp.Save(fileName); }
You can adjust the watermark text, font, color, position, and other properties according to your requirements.
You can also use the CreateWatermarkImage method above to create watermark images in asp net c#.