This documentation is archived and is not being maintained.

Graphics.MeasureCharacterRanges Method

Gets an array of Region objects, each of which bounds a range of character positions within the specified string.

[Visual Basic]
Public Function MeasureCharacterRanges( _
   ByVal text As String, _
   ByVal font As Font, _
   ByVal layoutRect As RectangleF, _
   ByVal stringFormat As StringFormat _
) As Region()
public Region[] MeasureCharacterRanges(
 string text,
 Font font,
 RectangleF layoutRect,
 StringFormat stringFormat
public: Region* MeasureCharacterRanges(
 String* text,
 Font* font,
 RectangleF layoutRect,
 StringFormat* stringFormat
) [];
public function MeasureCharacterRanges(
   text : String,
 font : Font,
 layoutRect : RectangleF,
 stringFormat : StringFormat
) : Region[];


String to measure.
Font object that defines the text format of the string.
RectangleF structure that specifies the layout rectangle for the string.
StringFormat object that represents formatting information, such as line spacing, for the string.

Return Value

This method returns an array of Region objects, each of which bounds a range of character positions within the specified string.


The regions returned by this method are resolution-dependent, so there might be a slight loss of accuracy if strings are recorded in a metafile at one resolution and later played back at a different resolution.


[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Sets a string "First and Second ranges" and a font for display of the string ("Times New Roman", 16pt.).
  • Sets two character ranges within the string (which correspond to the words "First" and "Second").
  • Creates a rectangle in which to display the string.
  • Sets the formatting of the string including the two character ranges.
  • Draws the string to the screen.
  • Measures the character ranges, determines rectangles that bound the two specified ranges.
  • Draws the two rectangles to the screen.

[Visual Basic, C#] The result is the displayed string with the first range ("First") bounded by a red rectangle and the second range ("Second") bounded by a blue rectangle.

[Visual Basic] 
Public Sub MeasureCharacterRangesRegions(e As PaintEventArgs)
' Set up string.
Dim measureString As String = "First and Second ranges"
Dim stringFont As New Font("Times New Roman", 16F)
' Set character ranges to "First" and "Second".
Dim characterRanges As CharacterRange() =  _
{New CharacterRange(0, 5), New CharacterRange(10, 6)}
' Create rectangle for layout.
Dim x As Single = 50F
Dim y As Single = 50F
Dim width As Single = 35F
Dim height As Single = 200F
Dim layoutRect As New RectangleF(x, y, width, height)
' Set string format.
Dim stringFormat As New StringFormat()
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
x, y, stringFormat)
' Measure two ranges in string.
Dim stringRegions(2) As [Region]
stringRegions = e.Graphics.MeasureCharacterRanges(measureString, _
stringFont, layoutRect, stringFormat)
' Draw rectangle for first measured range.
Dim measureRect1 As RectangleF = _
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), _
' Draw rectangle for second measured range.
Dim measureRect2 As RectangleF = _
e.Graphics.DrawRectangle(New Pen(Color.Blue, 1), _
End Sub
public void MeasureCharacterRangesRegions(PaintEventArgs e)
// Set up string.
string measureString = "First and Second ranges";
Font stringFont = new Font("Times New Roman", 16.0F);
// Set character ranges to "First" and "Second".
CharacterRange[] characterRanges =
new CharacterRange(0, 5),
new CharacterRange(10, 6)
// Create rectangle for layout.
float x = 50.0F;
float y = 50.0F;
float width = 35.0F;
float height = 200.0F;
RectangleF layoutRect = new RectangleF(x, y, width, height);
// Set string format.
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Draw string to screen.
x, y,
// Measure two ranges in string.
Region[] stringRegions = new Region[2];
stringRegions = e.Graphics.MeasureCharacterRanges(
// Draw rectangle for first measured range.
RectangleF measureRect1 = stringRegions[0].GetBounds(e.Graphics);
new Pen(Color.Red, 1),
// Draw rectangle for second measured range.
RectangleF measureRect2 = stringRegions[1].GetBounds(e.Graphics);
new Pen(Color.Blue, 1),

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Graphics Class | Graphics Members | System.Drawing Namespace