Pixelwidth of string with given font

  • Thread starter Thread starter Jesper
  • Start date Start date
J

Jesper

How do I query the pixelwidth of string with given font.
In the Font class I cant seem to find any function to help
me out.

Thanx.
Jesper.
 
SizeF oSize = new SizeF();

crFont = new Font("arial", 16, FontStyle.Bold);
Graphics g = get your graphics object
oSize = g.MeasureString("Your String to measure", crFont);

hth

Pete
 
Jesper,

You will want to use the MeasureString method on the Graphics instance
representing the device context that you want to display the string on. It
should give you the width, in pixels (for a screen) of the string you want
to draw on it.

Hope this helps.
 
How do I query the pixelwidth of string with given font.
In the Font class I cant seem to find any function to help
me out.

I use Graphics.MeasureString, if you're not measuring within the paint call
you'll have to create a graphics object.

n!
 
Be sure to read this KB article to know the cavets of GDI+ text output.

http://support.microsoft.com/?id=307208

Then consider using MeasureCharacterRanges if you need more accurate
results.

example:
textFormat.SetMeasurableCharacterRanges(new CharacterRange[] { new
CharacterRange(0, this.Text.Length()) } );

Region characterRegion = graphics.MeasureCharacterRanges(this.Text,
this.Font, new RectangleF(0, 0, float.MaxValue, float.MaxValue),
textFormat)[0];

ByronCullen
ThunderTools
 
Back
Top