public MeasureCharacterRanges ( string text, Font font, RectangleF layoutRect, StringFormat stringFormat ) : Region[] | ||
text | string | |
font | Font | |
layoutRect | RectangleF | |
stringFormat | StringFormat | |
return | Region[] |
using System.Drawing; using System.Drawing.Drawing2D; string str = "Hello, World!"; Font font = new Font("Arial", 12); using (Graphics g = CreateGraphics()) { SizeF size = g.MeasureString(str, font); RectangleF rect = new RectangleF(0, 0, size.Width, size.Height); CharacterRange[] ranges = new CharacterRange[str.Length]; for (int i = 0; i < str.Length; i++) { ranges[i] = new CharacterRange(i, 1); } StringFormat format = new StringFormat(); format.SetMeasurableCharacterRanges(ranges); Region[] regions = g.MeasureCharacterRanges(str, font, rect, format); foreach (Region region in regions) { RectangleF bounds = region.GetBounds(g); Console.WriteLine(bounds); } }In this example, we first create a string and a font to use for measuring. Then, we create a Graphics object using the CreateGraphics() method, which creates a Graphics object for the current form. We then use MeasureString() to calculate the size of the text string, and create a rectangle of that size. Next, we create an array of CharacterRange objects that represent each character in the string, and a StringFormat object that sets the measurable character ranges to these CharacterRanges. Finally, we use MeasureCharacterRanges() to calculate the bounding rectangles for each character, and print them out to the console. The package/library used in this example is the .NET System.Drawing.dll library.
public MeasureCharacterRanges ( string text, Font font, RectangleF layoutRect, StringFormat stringFormat ) : Region[] | ||
text | string | |
font | Font | |
layoutRect | RectangleF | |
stringFormat | StringFormat | |
return | Region[] |