Use MeasureCharacterRanges to see where the parts of a string will be drawn in C#

The Graphics class's MeasureCharacterRanges method can return Region objects that represent the locations where different pieces of a string would be drawn by DrawString. The following code finds regions representing each of a string's characters, draws the characters, and then draws rectangles around the regions.

// Draw the characters in a string and
// rectangles showing their locations.
private void Form1_Paint(object sender, PaintEventArgs e)
{
const string txt = "C# Helper!";

// Make the font.
using (Font the_font = new Font("Times New Roman", 40,
FontStyle.Bold | FontStyle.Italic))
{
// Make a StringFormat object to use for text layout.
using (StringFormat string_format = new StringFormat())
{
// Center the text.
string_format.Alignment = StringAlignment.Center;
string_format.LineAlignment = StringAlignment.Center;
string_format.FormatFlags = StringFormatFlags.NoClip;

// Make CharacterRanges to indicate which
// ranges we want to measure.
CharacterRange[] ranges = new CharacterRange[txt.Length];
for (int i = 0; i < txt.Length; i++)
{
ranges[i] = new CharacterRange(i, 1);
}
string_format.SetMeasurableCharacterRanges(ranges);

// Measure the text to see where each character range goes.
Region[] regions =
e.Graphics.MeasureCharacterRanges(
txt, the_font, this.ClientRectangle,
string_format);

// Draw the characters one at a time.
for (int i = 0; i < txt.Length; i++)
{
// See where this character would be drawn.
RectangleF rectf = regions[i].GetBounds(e.Graphics);
Rectangle rect = new Rectangle(
(int)rectf.X, (int)rectf.Y,
(int)rectf.Width, (int)rectf.Height);

// Draw the character.
e.Graphics.DrawString(txt.Substring(i, 1),
the_font, Brushes.Blue, rect, string_format);

// Draw a box where the character is.
e.Graphics.DrawRectangle(Pens.Black,
rect.Left, rect.Top, rect.Width, rect.Height);
}
}
}
}

The code first defines the text it will draw and makes the font it will use. It then makes a StringFormat object to determine how the text will be drawn. This object also determines the pieces of the string that MeasureCharacterRanges will measure. (That should probably have been a parameter to MeasureCharacterRanges rather than part of the StringFormat object but that's not the way Microsoft did it.)

The code sets the StringFormat's alignment properties and then creates an array of CharacterRange objects to pass to the StringFormat's SetMeasurableCharacterRanges method. Each CharacterRange gives the starting position and length of a part of the string to measure.

Next the code calls MeasureCharacterRanges. Finally the program loops through the characters, drawing each in its region and drawing a rectangle around the region.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.