Draw hollow text in C#

When the form loads, it sets ResizeRedraw to true so the form repaints itself whenever it is resized.
// Redraw on resize.
private void Form1_Load(object sender, EventArgs e)
{
this.ResizeRedraw = true;
}

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.

Next the form 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 then draws its outline with a thick black pen.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.