I'm using GDI+ to draw a string on a Graphics object.
I want the string to fit inside a pre-defined rectangle (without breaking any lines)
Is there's anyway of doing this besides using TextRenderer.MeasureString() in a loop until the desirable size is returned?
something like:
DrawScaledString(Graphics g, string myString, Rectangle rect)
You can use the ScaleTransform
But you might get some alias effects.
Edit:
But if you want to change the font size instead I guess you can change the font size with
scale
in the code above instead of using the scale transform. Try both and compare the quality of the result.Here's another solution to the problem, it's a little intensive as it requires a fair bit of font creation and destruction, but may work better, depending on your circumstances and needs:
To use it simply create a new class and call Render(). Note this is specifically written for rendering to a form.
You should create the RenderInBox object upfront due to it's intensive creation nature. Therefore it's not suitable for every need.