ElektroStudios
ElektroStudios

Reputation: 20474

'TextRenderer.MeasureText' giving distinct size than 'Graphics.MeasureString'

I suppose that exist a known clarification about why those methods always return distinct sizes, but anyways I will explain the problem a little:

I've subclassed a Progressbar, when I try to get the size of the same text with the same font, I get distinct values, TextRenderer.MeasureTextgives me a rounded value of 13 height and 300 width while Graphics.MeasureString gives me a very precise height of 13,xxxxxx and a minor width of 275,xxxxxxx

Why happens that?.

I'm not doing nothing especial in the code, just I've tried to use both methods to compare whether it gives me the same result or not to decide which to use in my code.

The overload of TextRenderer.MeasureText that I've tried to use is:

TextRenderer.MeasureText(String, Font)

And the Graphics.MeasureString method expects the same parameters:

Graphics.MeasureString(String, Font)

Upvotes: 0

Views: 410

Answers (1)

TextRenderer.MeasureText returns a Size so the values are integer. Graphics.MeasureString returns a SizeF containing floats.

Both are "raw values" though. When testing to see if some text will fit somewhere, you may also need to take into account Control.Padding which each Type may implement differently.

275 vs 300 seems like a large variance though. I have a checkbox thing that returns 82, 13 vs `82.85806, 13.8251925).

Upvotes: 1

Related Questions