How to scale text when drawing an image using C #

I would like to draw text in a rectangle and scale it to the maximum size that fits inside 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 text but does not increase the font size.

+3
source share
1 answer

Call Graphics.MeasureString in a binary search loop.

+1
source

Source: https://habr.com/ru/post/1782031/


All Articles