Search code examples
.netgraphicssystem.drawing

Draw a watermark string on image with double line fonts


I need to draw a watermark string on image with double line fonts. The font should be configurable, but the character should always be drawn using double lines. I'm attaching a image what the result image should look like:

watermark with double line fonts


Solution

  • You can get this by transforming text into a path and draw the path with a partially transparent pen. The core api to use is Graphics.AddText(). Here is a sample method that uses it:

        public static Bitmap Watermark(Image srce, string text, Font font, float angle) {
            Bitmap dest = new Bitmap(srce);
            var color = Color.FromArgb(120, Color.White);
            using (var gr = Graphics.FromImage(dest)) 
            using (var gp = new GraphicsPath())
            using (var pen = new Pen(color, 5)) {
                var sf = new StringFormat();
                sf.LineAlignment = sf.Alignment = StringAlignment.Center;
                gp.AddString(text, font.FontFamily, (int)font.Style, font.SizeInPoints, 
                             new Rectangle(-dest.Width/2, -dest.Height/2, dest.Width, dest.Height), 
                             sf);
                gr.TranslateTransform(dest.Width / 2, dest.Height / 2);
                gr.RotateTransform(-angle);
                gr.DrawPath(pen, gp);
            }
            return dest;
        }
    

    Sample usage:

        using (var bmp = Properties.Resources.Penguins) 
        using (var font = new Font(new FontFamily("Arial"), 144)) {
            pictureBox1.Image = Watermark(bmp, "DEMO", font, 45);
        }
    

    Which produces:

    enter image description here