Connecting Tech Pros Worldwide Forums | Help | Site Map

How to calulate width of string for given font

Andrus
Guest
 
Posts: n/a
#1: Apr 20 '07
I created RDL file containing TextBox with constant text.
I used the following code to find TextBox <Widthtag value.
However textbox width is too large.

How to find exact width of string for given string ?

Andrus.


float MeasureString(string stringToPaint, string fontName, int fontSize) {

Font font = new Font(fontName, fontSize, FontStyle.Regular);

// Set TextFormatFlags to no padding so strings are drawn together.
TextFormatFlags flags = TextFormatFlags.NoPadding |
TextFormatFlags.NoPrefix;

// Declare a proposed size with dimensions set to the maximum integer
value.
Size proposedSize = new Size(int.MaxValue, int.MaxValue);

Size size = TextRenderer.MeasureText( stringToPaint,
font, proposedSize, flags);
return size.Width;
}


Closed Thread