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.
arbiter
source share