dhrumilap
dhrumilap

Reputation: 142

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

Upvotes: 1

Views: 819

Answers (2)

Hans Passant
Hans Passant

Reputation: 942089

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

Upvotes: 5

Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11190

Always type your problem into some search engine, i found this looking for a similar answer:

http://www.neowin.net/forum/topic/269276-c-drawing-outline-text/

Upvotes: 0

Related Questions