How can I measure the width of the string which I draw on a Graphics object?
Printable View
How can I measure the width of the string which I draw on a Graphics object?
This way .
Code:protected override void OnPaint(PaintEventArgs e)
{
SizeF stringSize=new SizeF();
SizeF=e.Graphics.MeasureString("Your text here",stringSize,Myfont);
}
I dont know if it would be helpful but: just remember that SizeF is in decimal format, so if you are rounding it or converting it to integer I would suggest using Math.Ceiling. I was using this in VB and was a few pixels short depending on the system font size and couldnt figure it out, until I found out that it was a rounding problem:DQuote:
Originally posted by Pirate
This way .
Code:protected override void OnPaint(PaintEventArgs e)
{
SizeF stringSize=new SizeF();
SizeF=e.Graphics.MeasureString("Your text here",stringSize,Myfont);
}
Remembercan affect the result taken by MeasureString method .Code:StringAlignment