Click here to Skip to main content
6,634,665 members and growing! (16,793 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

Using MeasureCharacterRanges to Draw Text

By Gary Thom

How to use MeasureCharacterRanges to calculate the bounding rectangles of charaters in a string, to allow characters to be placed along curves.
C#, Windows, .NET 1.0, .NET 1.1, Visual Studio, Dev
Posted:10 Jun 2004
Views:44,590
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 2.94 Rating: 3.25 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
3 votes, 37.5%
4
2 votes, 25.0%
5

Sample Image - TryApp.gif

Introduction

This article will demonstrate the use of MeasureCharacterRanges to draw a string a character at a time. Not very exciting, I hear you cry. Bear with me.

Background

I wrote this short article after seeing a question posted on a newsgroup about being able to draw text along a curve. While drawing a character at a time is easy, I wanted to make sure the character spacing was maintained.

The code

Just set up some text to work with:

string measureString = "This is a test string.";
int    numChars         = measureString.Length;

Initialize the character ranges array, this is used to delimit the blocks of characters in the string, in this example, each character is a 'range'.

//

// Set up the characted ranger array.

Now, initialize the StringFormatFlags, I'm using the NoClips flag to ensure that the character is not clipped when drawing.

//

// Set up the string format

StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.NoClip;
stringFormat.SetMeasurableCharacterRanges(characterRanges);

Set up an array to hold the calculated regions:

//

// Set up the array to accept the regions.

Region[] stringRegions = new Region[numChars];
for(int i = 0; i<numChars; i++)
    characterRanges[i] = new CharacterRange(i, 1);

Create a font, and use MeasureCharacterRanges() to calculate the regions for the character ranges.

The regions returned by MeasureCharacterRanges are converted to rectangles by using the GetBounds() function. The rectangle can then be manipulated using offset or any other method to adjust its placement.

In this example, I offset the Y position using a random amount to give wavy text.

//

// The font to use.. 'using' will dispose of it for us

using (Font stringFont = new Font("Times New Roman", 16.0F))
{

    //

    // Get the max width.. for the complete length

    SizeF size = g.MeasureString(measureString, stringFont );

    //

    // Assume the string is in a stratight line, just to work out the 

    // regions. We will adjust the containing rectangles later.

    RectangleF layoutRect = 
        new RectangleF( 0.0f, 0.0f, size.Width, size.Height);

    //

    // Caluclate the regions for each character in the string.

    stringRegions = g.MeasureCharacterRanges(
        measureString,
        stringFont,
        layoutRect,
        stringFormat);

    //

    // Some random offsets, uncomment the DrawRectagle

    // if you want to see the bounding box.

    Random rand = new Random();
    for ( int indx = 0 ; indx < numChars; indx++ )
    {
        Region region = stringRegions[indx] as Region;
        RectangleF rect = region.GetBounds(g);
        rect.Offset( 0f, (float) rand.Next(100) / 10f);
        g.DrawString( measureString.Substring(indx,1), 
              stringFont, Brushes.Yellow, rect, stringFormat );
        // g.DrawRectangle( Pens.Red, Rectangle.Round( rect ));

    }
}

As I said at the beginning, this is not the most efficient code, the calculations could be done outside the drawing routine and cached. However, I hope this demonstrates how easy it is to determine the character positions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gary Thom


Member
I've been a developer since 1980 (has it been that long)..

Assembler --> C --> C++ --> C#

A few diversions along the way into Forth, Digital Signal Processing and control systems.

I was born in Scotland, now working in New York City.
Occupation: Web Developer
Location: United States United States

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralFinding Character Positions PinmemberMOHAMMADALARIQI3:54 2 Sep '09  
GeneralGDI+ Ressources Pinmembermaddin123412:20 21 Aug '04  
GeneralI found a way around the limitation :P PinmemberFocusedWolf8:24 16 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Jun 2004
Editor: Smitha Vijayan
Copyright 2004 by Gary Thom
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project