C# Source Code: Determining the width and height of a string
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Determining the width and height of a string
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, May 04, 2004
Hits:
735
Category:
Windows Forms/GUI/Controls
Article:
The code below returns the height and width of a string for a given font. ///
/// Return the text height and width of a specified string, given it's font. /// This routine is equivalent to TextHeight and TextWidth functions in VB. ///
void TextSize(string text, Font font, out float textWidth, out float textHeight) { using(Graphics g = this.CreateGraphics()) { SizeF textSize = g.MeasureString(text, font); textWidth = textSize.Width; textHeight = textSize.Height; } } //Demonstration routine void Test() { float txtHeight, txtWidth; Font fnt = new Font("MS Sans Serif",8); TextSize("TEST STRING", fnt, out txtWidth, out txtHeight); }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet