Windows Forms: How To Add Watermark Text To Images in C#

This post shows you How To Add Watermark Text To Images in C# Windows Forms Application.

Watermark is very useful when you are uploading images to website. It protects against copying images from the source website.

Dragging PictureBox, Button controls from the Visual Studio toolbox into your form designer, then design a simple UI allows you to create watermark on target image in c# with text as shown below.

c# add watermark to images

Adding 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 also use the CreateWatermarkImage method above to create watermark images in asp net c#.