Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-create-outlined-text.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

5.0 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Create Outlined Text 03/30/2017
csharp
vb
typography [WPF], linear gradient brush
outlined text [WPF]
gradient brush [WPF]
linear gradient brush [WPF]
typography [WPF], outline effects
4aa3cf6e-1953-4f26-8230-7c1409e5f28d

How to: Create outlined text

In most cases, when you're adding ornamentation to text strings in your [!INCLUDETLA#tla_winclient] application, you are using text in terms of a collection of discrete characters, or glyphs. For example, you could create a linear gradient brush and apply it to the xref:System.Windows.Controls.Control.Foreground%2A property of a xref:System.Windows.Controls.TextBox object. When you display or edit the text box, the linear gradient brush is automatically applied to the current set of characters in the text string.

Text displayed with a linear gradient brush

However, you can also convert text into xref:System.Windows.Media.Geometry objects, allowing you to create other types of visually rich text. For example, you could create a xref:System.Windows.Media.Geometry object based on the outline of a text string.

Text outline using a linear gradient brush

When text is converted to a xref:System.Windows.Media.Geometry object, it is no longer a collection of characters—you cannot modify the characters in the text string. However, you can affect the appearance of the converted text by modifying its stroke and fill properties. The stroke refers to the outline of the converted text; the fill refers to the area inside the outline of the converted text.

The following examples illustrate several ways of creating visual effects by modifying the stroke and fill of converted text.

Text with different colors for fill and stroke

Text with image brush applied to stroke

It is also possible to modify the bounding box rectangle, or highlight, of the converted text. The following example illustrates a way to create visual effects by modifying the stroke and highlight of converted text.

Text with image brush applied to stroke and highlight

Example

The key to converting text to a xref:System.Windows.Media.Geometry object is to use the xref:System.Windows.Media.FormattedText object. Once you have created this object, you can use the xref:System.Windows.Media.FormattedText.BuildGeometry%2A and xref:System.Windows.Media.FormattedText.BuildHighlightGeometry%2A methods to convert the text to xref:System.Windows.Media.Geometry objects. The first method returns the geometry of the formatted text; the second method returns the geometry of the formatted text's bounding box. The following code example shows how to create a xref:System.Windows.Media.FormattedText object and to retrieve the geometries of the formatted text and its bounding box.

[!code-csharpOutlineTextControlViewer#CreateText] [!code-vbOutlineTextControlViewer#CreateText]

In order to display the retrieved the xref:System.Windows.Media.Geometry objects, you need to access the xref:System.Windows.Media.DrawingContext of the object that's displaying the converted text. In these code examples, this access is achieved by creating a custom control object that's derived from a class that supports user-defined rendering.

To display xref:System.Windows.Media.Geometry objects in the custom control, provide an override for the xref:System.Windows.UIElement.OnRender%2A method. Your overridden method should use the xref:System.Windows.Media.DrawingContext.DrawGeometry%2A method to draw the xref:System.Windows.Media.Geometry objects.

[!code-csharpOutlineTextControlViewer#OnRender] [!code-vbOutlineTextControlViewer#OnRender]

For the source of the example custom user control object, see OutlineTextControl.cs for C# and OutlineTextControl.vb for Visual Basic.

See also