3

I would like to draw some text in a rectangle and have it scale to the maximum size that fits within the rectangle.

So far I have this:

    Bitmap bitmapImage = new Bitmap(500, 500);
    Graphics graphicImage = Graphics.FromImage(bitmapImage); 
    graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    var rect = new Rectangle(0, 0, 500, 500);

    graphicImage.DrawString( "testing testing 123!", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, rect);               
    bitmapImage.Save("test.png");       

it draws the text but doesn't scale up the font size.

1
  • 1
    No sign of Graphics.ScaleTransform() in your snippet. Finding the right arguments to pass to it is the // todo comment. Measure the string. Commented Dec 25, 2010 at 0:11

1 Answer 1

1

Call Graphics.MeasureString in a binary search loop.

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

2 Comments

Ouch, I was hoping for something in the .net framework instead of having to do it manually.
Hans mentioned ScaleTransform, in that case, draw the string once, measure it, then scale it, much simpler.

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.