You want to draw some text but display only its outline, and you want the text to have a drop shadow.
Sample code folder: Chapter 09\OutlineText
Use a GraphicsPath object to record the outside edge of a text string, and then use that outside edge, or path, to draw the actual drop shadow and outline elements.
Create a new Windows Forms application, and add a PictureBox control named PictureBox1 to the form. Set this control's BackColor property to White and its BorderStyle property to FixedSingle. Give it a size of approximately 400,150. Now add the following source code to the form's class template:
Private Sub PictureBox1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles PictureBox1.Paint ' ----- Draw text using an outline. Dim outlinePath As New Drawing2D.GraphicsPath Dim useFont As Font ' ----- Make some output adjustments to get a better ' outline. e.Graphics.TextRenderingHint = _ Drawing.Text.TextRenderingHint.AntiAlias e.Graphics.SmoothingMode = _ Drawing2D.SmoothingMode.AntiAlias ' ----- Draw the text into a path. useFont = New Font("Times New Roman", _ 96, FontStyle.Regular) outlinePath.AddString("Outline", useFont.FontFamily, _ FontStyle.Regular, 96, New Point(0, 0), _ StringFormat.GenericTypographic) useFont.Dispose() ' ----- Replay the path to draw a drop shadow. e.Graphics.TranslateTransform(25, 25) e.Graphics.FillPath(Brushes.LightGray, outlinePath) ' ----- Replay the path to the surface. e.Graphics.TranslateTransform(-5, -5) e.Graphics.FillPath(Brushes.White, outlinePath) e.Graphics.DrawPath(Pens.Black, outlinePath) ' ----- Finished. outlinePath.Dispose() End Sub
Running this program displays the outline and drop shadow shown in Figure 9-31.
Figure 9-31. Text in an outline form, with a drop shadow
While the Font class includes support for italic, bold, strikeout, and underline for-matting, it does not include features that automatically enable outlining or drop shadows. However, you can enable these features yourself using a GraphicsPath object. A graphics path is like a tape recording of a set of drawing commands that records the outline of the drawn elements. You use the GraphicsPath's drawing methods to record the outlines of shapes and text strings in the path. You can then later use this path like a macro that can be replayed on the graphics surface.
The GraphicsPath object's AddString() method adds the outer edge of all characters in the supplied text string to the path. There are additional methods that let you include other shapes, such as AddLine(), AddRectangle(), and AddEllipse().
Recipe 9.17 includes some similar alignment and rotation features.