[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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 the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.