Aziz
Aziz

Reputation: 1614

C# adding string to image, using the max font size

I want to add 2 string to an image as follows:

This is Text
------------
------------
------------
--Other-----

How do I use the largest font size possible without have the String go off the side of the image?

example: if text is too big then it goes off the image::

This is Text That is too big
------------
------------
------------
--Other-----

Upvotes: 2

Views: 1933

Answers (2)

Balazs Tihanyi
Balazs Tihanyi

Reputation: 6719

Here is a quick solution:

using (Graphics g = Graphics.FromImage(bmp))
{
    float width = g.MeasureString(text, font).Width;
    float scale = bmp.Width / width;
    g.ScaleTransform(scale, scale); //Simple trick not to use other Font instance
    g.DrawString(text, font, Brushes.Black, PointF.Empty);
    g.ResetTransform();
    ...
}

Your text won't be always 100% width if you use TextRenderingHint.AntiAliasGridFit or similar, but I think that's not a problem as you just want to make sure the text alawys fits in the image.

Upvotes: 1

Taha Paksu
Taha Paksu

Reputation: 15616

I wrote this script on my previous projects to fit some text into the image by calculating its dimensions for each font-size. when the font size is bigger than the image's width, it lowers the font size by 0.1em and tries again until the text fits in the image. Here's the code :

public static string drawTextOnMarker(string markerfile, string text, string newfilename,Color textColor)
    {
        //Uri uri = new Uri(markerfile, UriKind.Relative);
        //markerfile = uri.AbsolutePath;
        //uri = new Uri(newfilename, UriKind.Relative);
        //newfilename = uri.AbsolutePath;
        if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(newfilename)))
        {
            try
            {

                Bitmap bmp = new Bitmap(System.Web.HttpContext.Current.Server.MapPath(markerfile));
                Graphics g = Graphics.FromImage(bmp);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                StringFormat strFormat = new StringFormat();
                strFormat.Alignment = StringAlignment.Center;
                SolidBrush myBrush = new SolidBrush(textColor);
                float fontsize = 10;
                bool sizeSetupCompleted = false;
                while (!sizeSetupCompleted)
                {                        
                    SizeF mySize = g.MeasureString(text, new Font("Verdana", fontsize, FontStyle.Bold));
                    if (mySize.Width > 24 || mySize.Height > 13)
                    {
                        fontsize-= float.Parse("0.1");
                    }
                    else
                    {
                        sizeSetupCompleted = true;
                    }
                }

                g.DrawString(text, new Font("Verdana", fontsize, FontStyle.Bold), myBrush, new RectangleF(4, 3, 24, 8), strFormat);
                bmp.Save(System.Web.HttpContext.Current.Server.MapPath(newfilename));
                return newfilename.Substring(2);
            }
            catch (Exception)
            {
                return markerfile.Substring(2);
            }
        }
        return newfilename.Substring(2);
    }

Upvotes: 3

Related Questions