How can I calculate the height needed to render a text wrapped in a specified width?
I found the following method in Graphics
graphicsObj.MeasureString(text, font, width);
But it needs an instance of Graphics
and at the time I have not graphics instance. In fact I prefer a static method to find the height. TextRenderer.MeasureText(..)
could be an option but it lacks a parameter for proposed width.
The overload & flag you need is:
var size = TextRenderer.MeasureText(text, font, new Size(width, height), TextFormatFlags.WordBreak);
For accuracy you should really try to use one of the overloads that accepts a device context.
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