C# Programming > Drawing

C# Draw Text Outline

String Outline

While it is not obvious how to draw a text outline in C#, it is possible to do so with only GDI+ and no fancy algorithms. The only thing required is thinking of text in C# as paths, not as strings.

Paths in .NET drawing are a powerful way to draw all types of shapes.

DrawString

The way most programmers know how to draw text is with the DrawString GDI+ function. DrawString is accessible through a normal Graphics object which is usually initialized in common ways such as this.createGraphics();

DrawString is a very easy to use GDI+ method. The quality of the text would usually be controlled through something like the TextRenderingHint property.

However you will notice that DrawString only draws text that is filled in. If we only want to outline the text, we have to use something else.

DrawPath

The answer is to create a GraphicsPath object from the System.Drawing.Drawing2D namespace. A GraphicsPath is a very powerful GDI+ class that lets programmers draw complex objects. One of which happens to be text, or strings. The AddString function in a GraphicsPath is very similar to DrawString. This is the method you are going to use instead. The parameters a little different, for StringFormat, if you don't want to deal with it, just use StringFormat.GenericDefault.

Once you have GraphicsPath that contains the text, you can draw it using either FillPath or DrawPath. FillPath will yield similar results as DrawString.

DrawPath on the other hand is going to draw an outline of the text. Notice how the flexibility of having both GDI+ functions makes the possibilities endless. The only thing to remember is that the SmoothingMode property will now be the one that affects the quality of the text.

Back to C# Article List