--- title: "Optimizing Performance: Text" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "rendering text [WPF]" - "hyperlinks [WPF]" - "formatted text [WPF]" - "text [WPF], performance" - "glyphs [WPF]" ms.assetid: 66b1b9a7-8618-48db-b616-c57ea4327b98 --- # Optimizing Performance: Text [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] includes support for the presentation of text content through the use of feature-rich [!INCLUDE[TLA#tla_ui](../../../includes/tlasharptla-ui-md.md)] controls. In general you can divide text rendering in three layers: 1. Using the and objects directly. 2. Using the object. 3. Using high-level controls, such as the and objects. This topic provides text rendering performance recommendations. ## Rendering Text at the Glyph Level [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)] provides advanced text support including glyph-level markup with direct access to for customers who want to intercept and persist text after formatting. These features provide critical support for the different text rendering requirements in each of the following scenarios. - Screen display of fixed-format documents. - Print scenarios. - [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)] as a device printer language. - Microsoft XPS Document Writer. - Previous printer drivers, output from Win32 applications to the fixed format. - Print spool format. - Fixed-format document representation, including clients for previous versions of Windows and other computing devices. > [!NOTE] > and are designed for fixed-format document presentation and print scenarios. [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)] provides several elements for general layout and [!INCLUDE[TLA#tla_ui](../../../includes/tlasharptla-ui-md.md)] scenarios such as and . For more information on layout and [!INCLUDE[TLA2#tla_ui](../../../includes/tla2sharptla-ui-md.md)] scenarios, see the [Typography in WPF](typography-in-wpf.md). The following examples show how to define properties for a object in [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)]. The object represents the output of a in [!INCLUDE[TLA2#tla_xaml](../../../includes/tla2sharptla-xaml-md.md)]. The examples assume that the Arial, Courier New, and Times New Roman fonts are installed in the **C:\WINDOWS\Fonts** folder on the local computer. [!code-xaml[GlyphsOvwSample1#1](~/samples/snippets/csharp/VS_Snippets_Wpf/GlyphsOvwSample1/CS/default.xaml#1)] ### Using DrawGlyphRun If you have custom control and you want to render glyphs, use the method. [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] also provides lower-level services for custom text formatting through the use of the object. The most efficient way of rendering text in [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)] is by generating text content at the glyph level using and . However, the cost of this efficiency is the loss of easy to use rich text formatting, which are built-in features of [!INCLUDE[TLA#tla_winclient](../../../includes/tlasharptla-winclient-md.md)] controls, such as and . ## FormattedText Object The object allows you to draw multi-line text, in which each character in the text can be individually formatted. For more information, see [Drawing Formatted Text](drawing-formatted-text.md). To create formatted text, call the constructor to create a object. Once you have created the initial formatted text string, you can apply a range of formatting styles. If your application wants to implement its own layout, then the object is better choice than using a control, such as . For more information on the object, see [Drawing Formatted Text](drawing-formatted-text.md) . The object provides low-level text formatting capability. You can apply multiple formatting styles to one or more characters. For example, you could call both the and methods to change the formatting of the first five characters in the text. The following code example creates a object and renders it. [!code-csharp[formattedtextsnippets#FormattedTextSnippets1](~/samples/snippets/csharp/VS_Snippets_Wpf/FormattedTextSnippets/CSharp/Window1.xaml.cs#formattedtextsnippets1)] [!code-vb[formattedtextsnippets#FormattedTextSnippets1](~/samples/snippets/visualbasic/VS_Snippets_Wpf/FormattedTextSnippets/visualbasic/window1.xaml.vb#formattedtextsnippets1)] ## FlowDocument, TextBlock, and Label Controls [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] includes multiple controls for drawing text to the screen. Each control is targeted to a different scenario and has its own list of features and limitations. ### FlowDocument Impacts Performance More than TextBlock or Label In general, the element should be used when limited text support is required, such as a brief sentence in a [!INCLUDE[TLA#tla_ui](../../../includes/tlasharptla-ui-md.md)]. can be used when minimal text support is required. The element is a container for re-flowable documents that support rich presentation of content, and therefore, has a greater performance impact than using the or controls. For more information on , see [Flow Document Overview](flow-document-overview.md). ### Avoid Using TextBlock in FlowDocument The element is derived from . The element is derived from , which is less costly to use than a -derived object. When possible, use rather than for displaying text content in a . The following markup sample illustrates two ways of setting text content within a : [!code-xaml[Performance#PerformanceSnippet13](~/samples/snippets/csharp/VS_Snippets_Wpf/Performance/CSharp/FlowDocument.xaml#performancesnippet13)] ### Avoid Using Run to Set Text Properties In general, using a within a is more performance intensive than not using an explicit object at all. If you are using a in order to set text properties, set those properties directly on the instead. The following markup sample illustrates these two ways of setting a text property, in this case, the property: [!code-xaml[Performance#PerformanceSnippet12](~/samples/snippets/csharp/VS_Snippets_Wpf/Performance/CSharp/Window1.xaml#performancesnippet12)] The following table shows the cost of displaying 1000 objects with and without an explicit . |**TextBlock type**|**Creation time (ms)**|**Render time (ms)**| |------------------------|------------------------------|----------------------------| |Run setting text properties|146|540| |TextBlock setting text properties|43|453| ### Avoid Databinding to the Label.Content Property Imagine a scenario where you have a object that is updated frequently from a source. When data binding the element's property to the source object, you may experience poor performance. Each time the source is updated, the old object is discarded and a new is recreated—because a object is immutable, it cannot be modified. This, in turn, causes the of the object to discard its old content and regenerate the new content to display the new . The solution to this problem is simple. If the is not set to a custom value, replace the with a and data bind its property to the source string. |**Data bound property**|**Update time (ms)**| |-----------------------------|----------------------------| |Label.Content|835| |TextBlock.Text|242| ## Hyperlink The object is an inline-level flow content element that allows you to host hyperlinks within the flow content. ### Combine Hyperlinks in One TextBlock Object You can optimize the use of multiple elements by grouping them together within the same . This helps to minimize the number of objects you create in your application. For example, you may want to display multiple hyperlinks, such as the following: MSN Home | My MSN The following markup example shows multiple elements used to display the hyperlinks: [!code-xaml[Performance#PerformanceSnippet9](~/samples/snippets/csharp/VS_Snippets_Wpf/Performance/CSharp/Hyperlink.xaml#performancesnippet9)] The following markup example shows a more efficient way of displaying the hyperlinks, this time, using a single : [!code-xaml[Performance#PerformanceSnippet10](~/samples/snippets/csharp/VS_Snippets_Wpf/Performance/CSharp/Hyperlink.xaml#performancesnippet10)] ### Showing Underlines on Hyperlinks Only on MouseEnter Events A object is a visual ornamentation that you can add to text; however, it can be performance intensive to instantiate. If you make extensive use of elements, consider showing an underline only when triggering an event, such as the event. For more information, see [Specify Whether a Hyperlink is Underlined](how-to-specify-whether-a-hyperlink-is-underlined.md). The following image shows how the MouseEnter event triggers the underlined hyperlink: ![Hyperlinks displaying TextDecorations](./media/how-to-specify-whether-a-hyperlink-is-underlined/text-decorations-hyperlinks.png) The following markup sample shows a defined with and without an underline: [!code-xaml[Performance#PerformanceSnippet11](~/samples/snippets/csharp/VS_Snippets_Wpf/Performance/CSharp/Hyperlink.xaml#performancesnippet11)] The following table shows the performance cost of displaying 1000 elements with and without an underline. |**Hyperlink**|**Creation time (ms)**|**Render time (ms)**| |-------------------|------------------------------|----------------------------| |With underline|289|1130| |Without underline|299|776| ## Text Formatting Features [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] provides rich text formatting services, such as automatic hyphenations. These services may impact application performance and should only be used when needed. ### Avoid Unnecessary Use of Hyphenation Automatic hyphenation finds hyphen breakpoints for lines of text, and allows additional break positions for lines in and objects. By default, the automatic hyphenation feature is disabled in these objects. You can enable this feature by setting the object's IsHyphenationEnabled property to `true`. However, enabling this feature causes [!INCLUDE[TLA2#tla_winclient](../../../includes/tla2sharptla-winclient-md.md)] to initiate Component Object Model (COM) interoperability, which can impact application performance. It is recommended that you do not use automatic hyphenation unless you need it. ### Use Figures Carefully A element represents a portion of flow content that can be absolutely-positioned within a page of content. In some cases, a may cause an entire page to automatically reformat if its position collides with content that has already been laid-out. You can minimize the possibility of unnecessary reformatting by either grouping elements next to each other, or declaring them near the top of content in a fixed page size scenario. ### Optimal Paragraph The optimal paragraph feature of the object lays out paragraphs so that white space is distributed as evenly as possible. By default, the optimal paragraph feature is disabled. You can enable this feature by setting the object's property to `true`. However, enabling this feature impacts application performance. It is recommended that you do not use the optimal paragraph feature unless you need it. ## See also - [Optimizing WPF Application Performance](optimizing-wpf-application-performance.md) - [Planning for Application Performance](planning-for-application-performance.md) - [Taking Advantage of Hardware](optimizing-performance-taking-advantage-of-hardware.md) - [Layout and Design](optimizing-performance-layout-and-design.md) - [2D Graphics and Imaging](optimizing-performance-2d-graphics-and-imaging.md) - [Object Behavior](optimizing-performance-object-behavior.md) - [Application Resources](optimizing-performance-application-resources.md) - [Data Binding](optimizing-performance-data-binding.md) - [Other Performance Recommendations](optimizing-performance-other-recommendations.md)