Draw hollow text in C#


hollow text

When the form loads, it sets ResizeRedraw to true so the form automatically repaints itself whenever it is resized.

// Redraw on resize.
private void Form1_Load(object sender, EventArgs e)
{
    this.ResizeRedraw = true;
}

Strangely enough, you can’t set ResizeRedraw at design time. You can only set it in code.

The form’s Paint event handler draws the hollow text.

private void Form1_Paint(object sender, PaintEventArgs e)
{
    // Make things smoother.
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    // Create the text path.
    GraphicsPath path = new GraphicsPath(FillMode.Alternate);

    // Draw text using a StringFormat to center it on the form.
    using (FontFamily font_family =
        new FontFamily("Times New Roman"))
    {
        using (StringFormat sf = new StringFormat())
        {
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            path.AddString("Hollow Text", font_family,
                (int)FontStyle.Bold, 100,
                this.ClientRectangle, sf);
        }
    }

    // Fill and draw the path.
    e.Graphics.FillPath(Brushes.Blue, path);
    using (Pen pen = new Pen(Color.Black, 3))
    {
        e.Graphics.DrawPath(pen, path);
    }
}

The code starts by setting SmoothingMode to AntiAlias to get smoother curves. It then makes a GraphicsPath object and adds text to it. It uses a StringFormat object to center the text on the form’s client area.

The code then fills the GraphicsPath with a blue brush and draws its outline with a thick black pen.


Download Example   Follow me on Twitter   RSS feed




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

One Response to Draw hollow text in C#

  1. Pingback: Draw text filled with text in C# -

Leave a Reply

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