Rendering Text with Quality and Performance

In Chapter 3 (Section 3.1) I said that we would discuss some of the Graphics class members in later chapters. Here we will discuss the TextRenderingHint property of the Graphics class.

Note

The TextRenderingHint enumeration is defined in the System.Drawing.Text namespace.

The TextRenderingHint property of the Graphics class defines the quality of text rendered on graphics surfaces. The quality also affects drawing performance. For best performance, select low-quality rendering. Better quality will produce slower rendering. For LCD displays, ClearType text provides the best quality.

The TextRenderingHint property takes a value of type TextRenderingHint enumeration. The members of the TextRenderingHint enumeration are described in Table 5.13.

Listing 5.12 uses the TextRenderingHint property to draw text with different options. This code draws four different text strings using different text rendering hint options.

Listing 5.12 Using TextRenderingHint to set the quality of text

private void menuItem17_Click(object sender, System.EventArgs e) { Graphics g = this.CreateGraphics(); g.Clear(this.BackColor); SolidBrush redBrush = new SolidBrush(Color.Red); Font verdana16 = new Font("Verdana", 16); string text1 = "Text with SingleBitPerPixel"; string text2 = "Text with ClearTypeGridFit"; string text3 = "Text with AntiAliasing"; string text4 = "Text with SystemDefault"; // Set TextRenderingHint property of surface // to single bit per pixel g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel; // Draw string g.DrawString(text1, verdana16, redBrush, new PointF(10, 10)); // Set TextRenderingHint property of surface // to ClearType grid fit g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; // Draw string g.DrawString(text2, verdana16, redBrush, new PointF(10, 60)); // Set TextRenderingHint property of surface // to AntiAlias g.TextRenderingHint = TextRenderingHint.AntiAlias; // Draw string g.DrawString(text3, verdana16, redBrush, new PointF(10, 100)); // Set TextRenderingHint property of surface // to SystemDefault g.TextRenderingHint = TextRenderingHint.SystemDefault; // Draw string g.DrawString(text4, verdana16, redBrush, new PointF(10, 150)); // Dispose of objects redBrush.Dispose(); g.Dispose(); }

Table 5.13. TextRenderingHint members

Member

Description

AntiAlias

Characters are rendered by anti-aliasing without hinting. AntiAlias offers good quality, but slow performance.

AntiAliasGridFit

Characters are anti-aliased with hinting. AntiAliasGridFit offers good quality and high performance.

ClearTypeGridFit

Characters are drawn by a ClearType bitmap with hinting. This is the highest-quality setting, with slow performance. It takes advantage of ClearType font features, if available.

SingleBitPerPixel

Characters are drawn with each glyph's bitmap. Hinting is not used.

SingleBitPerPixelGridFit

Characters are drawn with each glyph's bitmap. Hinting is used to improve character appearance on stems and curvature.

SystemDefault

Characters are drawn with each glyph's bitmap, with the system's default rendering hint.

Figure 5.17 shows the output from Listing 5.12. Different TextRenderingHint options result in text with higher or lower quality. (How clearly this shows up will vary on different displaysand it may be hard to see in print.)

Figure 5.17. Using different TextRenderingHint settings to draw text

Категории