TextRenderer.MeasureText Results Accuracy

Call TextRenderer.MeasureText as follows:

TextRenderer.MeasureText(myControl.Text, myControl.Font); 

and comparing the result with the size of the control to check if the text is suitable. The results are sometimes incorrect. The following two questions were observed:

  • Often, when the label is set to Auto Scale, the TextRenderer will report a width that is 1 pixel wider than the automatic size of the control.
  • False negative, where TextRenderer reports a width smaller than the control, but the text is still disabled. This happened with the "Estaciรณn de trabajo" - not sure if the emphasis can somehow influence the calculation of the width?

Is there a way to improve the accuracy of the MeasureText method? Should I call one of the overrides that accept device context flags and / or format?

+2
c # winforms
source share
4 answers

Is there a way to improve the accuracy of the MeasureText method? Should I call one of the overrides that accept device context flags and / or format?

You yourself answered your own question. In fact, MeasureText is based on Win32 DrawTextEx, and this function cannot work without a valid device context. Therefore, when you call an override of the MeasureText parameter without hdc, it internally creates the hdc-compatible desktop for the measurement.

Of course, the measurement depends on additional TextFormatFlags. Also keep in mind that the sticker (and dimension) is dependent on UseCompatibleTextRendering .

In general, you should use MeasureText for your own code, for example, when you call DrawText with exactly the same parameters, in all other cases the size returned by MeasureText cannot be considered accurate.

If you need to get the expected label size, you should use the GetPreferredSize method.

+4
source share

I know that this is probably no longer relevant. However, for future readers, there is a simple but accurate method for measuring text in a control:

 Graphics g=Graphics.FromHwnd(YOUR CONTROL HERE.Handle); SizeF s=g.MeasureString("YOUR STRING HERE", Font, NULL, NULL, STRING LENGTH HERE, 1) 
+7
source share

I donโ€™t know if I have the perfect solution, but I came across this when I was doing WinForms several years ago. The way I finished the compensation was by adjusting the returned measurement by a percentage. I canโ€™t remember what I used (maybe 5% or 105?), But I remember that in the end I used a constant percentage in the application and was always rounded.

+1
source share

Check the TextFormatFlags parameter for this function:

 TextRenderer::MeasureText(String, Font, Size, TextFormatFlags) 

http://msdn.microsoft.com/en-us/library/8wafk2kt.aspx

"The pixel size of the text drawn on the same line with the specified font. You can manipulate how the text is drawn using one of the DrawText overloads, which accepts the TextFormatFlags parameter. For example, the default behavior of the TextRenderer should add an addition to the bounding box of the drawn text to accommodate overhanging glyphs.If you need to draw a line of text without these extra spaces, you should use the DrawText and MeasureText versions that accept the Size and TextFormatFlags options.example, see MeasureTex t (IDeviceContext, String, Font, Size, TextFormatFlags).

Hth

+1
source share

All Articles