This is not about the size of a string.
I specify a font for drawing strings. And now I need the exact width of 1 character in pixels. I only allow fonts that have static char-width (W and i have the same width when drawn). If I use "Consolas" with size of 10 I get an estimated width of 7.75f
. Can I retrieve this information directly somehow ?
I don't need this information while drawing but when I want to set cursor position etc. So I don't know what text is beneath cursor.
Edit: MeasureString gives me 11.99653 as char width. Dunno why. But I can't use this value. I estimated 7.75f and it's pretty exact. I'll get in trouble if I want to change font size.
Use the MeasureString
method:
Graphics g = this.CreateGraphics();
g.MeasureString("A", font, 10, StringFormat.GenericTypographic);
where font
is an instance of the Font
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With