Op Englesch liesen Editéieren

Deelen iwwer


FormattedText.BuildGeometry(Point) Method

Definition

Returns a Geometry object that represents the formatted text, including all glyphs and text decorations.

C#
public System.Windows.Media.Geometry BuildGeometry(System.Windows.Point origin);

Parameters

origin
Point

The top-left origin of the resulting geometry.

Returns

The Geometry object representation of the formatted text.

Examples

The following example shows how to create a FormattedText object and to retrieve the geometries of the formatted text and its bounding box.

C#
/// <summary>
/// Create the outline geometry based on the formatted text.
/// </summary>
public void CreateText()
{
    System.Windows.FontStyle fontStyle = FontStyles.Normal;
    FontWeight fontWeight = FontWeights.Medium;

    if (Bold) fontWeight = FontWeights.Bold;
    if (Italic) fontStyle = FontStyles.Italic;

    // Create the formatted text based on the properties set.
    FormattedText formattedText = new FormattedText(
        Text,
        CultureInfo.GetCultureInfo("en-us"),
        FlowDirection.LeftToRight,
        new Typeface(
            Font,
            fontStyle,
            fontWeight,
            FontStretches.Normal),
        FontSize,
        System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text. 
        );

    // Build the geometry object that represents the text.
    _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0));

    // Build the geometry object that represents the text highlight.
    if (Highlight)
    {
        _textHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0));
    }
}

Remarks

When text is converted to a 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 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 Example of setting stroke and fill to different colors

Text with image brush applied to stroke Example of an image brush applied to the stroke

Text that is converted to and rendered as a Geometry object may not look the same as text rendered directly:

  • Text converted to a Geometry object is not rendered using ClearType. In addition, the baseline of the converted is not snapped to a whole display pixel.

  • Small fonts, such as those commonly used in body text, may lose legibility, appear blurred, and vary in appearance.

Applies to

Produkt Versiounen
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10