Draw a watermark in C#


[watermark]

The example Use a ColorMatrix to add a watermark to an image in C# shows one way to add a watermark to an image. This example shows another technique that is efficient and easy to understand. It also lets you easily change how dark the watermark is so you can determine how much it obscures the background image.

The basic approach is to use two brushes. The brushes both have alpha values less than 255 so they are translucent. The program first draws the text using a translucent black brush and then draws it again shifted two pixels left and up with a translucent white brush. The final picture is slightly darkened along the lower and right edges of the text and lightened on the text’s body.

This example uses the following code to draw watermarks with different levels of opacity.

// Draw translucent text.
private void Form1_Load(object sender, EventArgs e)
{
    Bitmap bm = new Bitmap(picSrc.Image);
    using (Graphics gr = Graphics.FromImage(bm))
    {
        using (StringFormat string_format = new StringFormat())
        {
            string_format.Alignment = StringAlignment.Center;

            int dy = (int)(gr.MeasureString("X", this.Font)
                .Height * 1.2);
            int x = bm.Width / 2;
            int y = 20;

            for (int opacity = 20; opacity <= 80; opacity += 10)
            {
                string txt = "OPACITY " + opacity.ToString();
                using (Brush brush = new SolidBrush(
                    Color.FromArgb(opacity, 0, 0, 0)))
                {
                    gr.DrawString(txt, this.Font,
                        brush, x, y, string_format);
                }
                using (Brush brush = new SolidBrush(
                    Color.FromArgb(opacity, 255, 255, 255)))
                {
                    gr.DrawString(txt, this.Font,
                        brush, x - 2, y - 2, string_format);
                }
                y += dy;
            }
        }

        picResult.Image = bm;
    }
}

The code first makes a copy of the image on the left and creates a Graphics object to work with the copy. It then makes a StringFormat object that centers text horizontally.

Next the code measures the string “X” so it can tell how tall the text will be. It sets variable dy to represent the line height it will use and initializes x and y to position the first line of text.

The code then enters a loop that uses opacitites 20, 30, 40, …, 80. For each opacity, the program draws the text twice as described above to make a watermark. It then adds dy to y so the next line is moved down and it repeats the loop.

You can use the picture at the top of the post to decide what level of opacity looks best. Depending on how obvious you want the watermark to be, an opacity of 20 or 30 may be dark enough.

Feel free to experiment with other opacities, shifting the text by different amounts (try shifting by five pixels instead of 2), and colored brushes (try giving the second brush the color Color.FromArgb(opacity, 255, 128, 128)).


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in drawing, fonts, graphics, image processing and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *