Setting Digital Substitution in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

The SetDigitSubstitution method can be used to substitute digits in a string on the basis of a user's local area. SetDigitSubstitution takes a parameter of the SetDigitSubstitution enumeration, the members of which are described in Table 5.12.

TABLE 5.12: StringDigitSubstitute members

Members

Description

National

Provides substitutions digits based on the national language of the user's locale.

None

Disables substitutions.

Traditional

Provides substitution digits based on user's native script or language.

User

Provides a user-defined substitution.

Rendering Text with Quality and Performance

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. The code draws four different text strings using different text rendering hint options.

LISTING 5.12: Using TextRenderingHint to set the quality of text

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Drawing.Text;
using
System.Linq;
using
System.IO;
using
System.Text;
using
System.Windows.Forms;

namespace
WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs 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 to 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.12: 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 ClearType bitmap with hinting. This is the highest-quality settings, 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 with higher or lower quality. (How clearly this shows up will vary on different displays-and it may be hard to see in print.)

fig5.17.gif

FIGURE 5.17: Using different TextRenderingHint setting to draw text

Conclusion

Hope the article would have helped you in understanding Setting Digital Substitution in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.