0

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)
5
  • 1
    Because TextRender returns a Size, MeasureString returns SizeF. Depending on where you are putting the Text, one may be more appropriate than the other... the FONT arg is usually more like thisCtl.Font so that it is accurate. Commented Jul 15, 2014 at 17:31
  • Oh, was strange 'cause both returned me a Single, but yes I suppse that the Size/SizeFloat explains everything... just I wanted to avoid instancing a Graphics object just to measure the text inside the control, do you know another method that gives a SizeF structure without need of creating a graphics object?. PS: like always I say feel free to post that as an answer to mark it Commented Jul 15, 2014 at 17:34
  • 1
    A control shouldnt need to use MeasureString, but 275 vs 300 seems wrong, which is why I mentioned the Font thing. You also might want to include the padding stuff for the control. Commented Jul 15, 2014 at 17:38
  • Ok, thanks, it's solved. PS: I've used the same font to test them, the font used by the control Commented Jul 15, 2014 at 17:40
  • 1
    You might want to look at ProgressBarRenderer if you are drawing to one there might be internal margins defined which will alter your results. nevermind - there are none for it. The CheckBoxRender gives you the size of the check and margin between it and the start of text. Commented Jul 15, 2014 at 17:42

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.