Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Graphics Class
Graphics Methods
 MeasureCharacterRanges Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Graphics..::.MeasureCharacterRanges Method

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

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
Visual Basic (Declaration)
Public Function MeasureCharacterRanges ( _
    text As String, _
    font As Font, _
    layoutRect As RectangleF, _
    stringFormat As StringFormat _
) As Region()
Visual Basic (Usage)
Dim instance As Graphics
Dim text As String
Dim font As Font
Dim layoutRect As RectangleF
Dim stringFormat As StringFormat
Dim returnValue As Region()

returnValue = instance.MeasureCharacterRanges(text, _
    font, layoutRect, stringFormat)
C#
public Region[] MeasureCharacterRanges(
    string text,
    Font font,
    RectangleF layoutRect,
    StringFormat stringFormat
)
Visual C++
public:
array<Region^>^ MeasureCharacterRanges(
    String^ text, 
    Font^ font, 
    RectangleF layoutRect, 
    StringFormat^ stringFormat
)
JScript
public function MeasureCharacterRanges(
    text : String, 
    font : Font, 
    layoutRect : RectangleF, 
    stringFormat : StringFormat
) : Region[]

Parameters

text
Type: System..::.String
String to measure.
font
Type: System.Drawing..::.Font
Font that defines the text format of the string.
layoutRect
Type: System.Drawing..::.RectangleF
RectangleF structure that specifies the layout rectangle for the string.
stringFormat
Type: System.Drawing..::.StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.

Return Value

Type: array<System.Drawing..::.Region>[]()[]
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.

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.

Visual Basic
    Private Sub MeasureCharacterRangesRegions(ByVal e As PaintEventArgs)

        ' Set up string.
        Dim measureString As String = "First and Second ranges"
        Dim stringFont As New Font("Times New Roman", 16.0F)

        ' 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 = 50.0F
        Dim y As Single = 50.0F
        Dim width As Single = 35.0F
        Dim height As Single = 200.0F
        Dim layoutRect As New RectangleF(x, y, width, height)

        ' Set string format.
        Dim stringFormat As 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.
        Dim stringRegions() As [Region] = e.Graphics.MeasureCharacterRanges(measureString, _
    stringFont, layoutRect, stringFormat)

        ' Draw rectangle for first measured range.
        Dim measureRect1 As RectangleF = _
        stringRegions(0).GetBounds(e.Graphics)
        e.Graphics.DrawRectangle(New Pen(Color.Red, 1), _
        Rectangle.Round(measureRect1))

        ' Draw rectangle for second measured range.
        Dim measureRect2 As RectangleF = _
        stringRegions(1).GetBounds(e.Graphics)
        e.Graphics.DrawRectangle(New Pen(Color.Blue, 1), _
        Rectangle.Round(measureRect2))
    End Sub
C#
        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));
        }
Visual C++
public:
   void MeasureCharacterRangesRegions( PaintEventArgs^ e )
   {
      // Set up string.
      String^ measureString = "First and Second ranges";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Times New Roman",16.0F );

      // Set character ranges to "First" and "Second".
      array<CharacterRange>^ characterRanges = {CharacterRange(0,5),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 = RectangleF(x,y,width,height);

      // Set string format.
      StringFormat^ stringFormat = gcnew 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.
      array<System::Drawing::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( gcnew Pen( Color::Red,1.0f ), Rectangle::Round( measureRect1 ) );

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker