Graphics.MeasureCharacterRanges Method

Definition

Overloads

MeasureCharacterRanges(ReadOnlySpan<Char>, Font, RectangleF, StringFormat)

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

MeasureCharacterRanges(String, Font, RectangleF, StringFormat)

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

MeasureCharacterRanges(ReadOnlySpan<Char>, Font, RectangleF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public System.Drawing.Region[] MeasureCharacterRanges (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat? stringFormat);

Parameters

text
ReadOnlySpan<Char>

String to measure.

font
Font

Font that defines the text format of the string.

layoutRect
RectangleF

RectangleF structure that specifies the layout rectangle for the string.

stringFormat
StringFormat

StringFormat that represents formatting information, such as line spacing, for the string.

Returns

Region[]

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

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
Windows Desktop 8, 9

MeasureCharacterRanges(String, Font, RectangleF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

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

public System.Drawing.Region[] MeasureCharacterRanges (string? text, System.Drawing.Font font, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat? stringFormat);
public System.Drawing.Region[] MeasureCharacterRanges (string text, System.Drawing.Font font, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat stringFormat);

Parameters

text
String

String to measure.

font
Font

Font that defines the text format of the string.

layoutRect
RectangleF

RectangleF structure that specifies the layout rectangle for the string.

stringFormat
StringFormat

StringFormat that represents formatting information, such as line spacing, for the string.

Returns

Region[]

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

Exceptions

font is null.

Examples

The following code 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", 16 point).

  • 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.

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.

private 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;
    stringFormat.SetMeasurableCharacterRanges(characterRanges);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, x, y, stringFormat);

    // Measure two ranges in string.
    Region[] stringRegions = e.Graphics.MeasureCharacterRanges(measureString, 
stringFont, layoutRect, stringFormat);

    // Draw rectangle for first measured range.
    RectangleF measureRect1 = stringRegions[0].GetBounds(e.Graphics);
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), Rectangle.Round(measureRect1));

    // Draw rectangle for second measured range.
    RectangleF measureRect2 = stringRegions[1].GetBounds(e.Graphics);
    e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), Rectangle.Round(measureRect2));
}

Remarks

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.

See also

Applies to

.NET 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9