2

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-----

2 Answers 2

3

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);
    }
Sign up to request clarification or add additional context in comments.

5 Comments

fontsize-= float.Parse("0.1");, why not just do fontsize -= 1.0f?
because I was learning C# and that was my first month. :) They do the same at the end. For example I was using String mystring = ""; and my friend was using String mystring = String.Empty(); and when I ask why do you do like this, he said that he was used to it. Different methods, same results.
and BTW, the mysize.width > 24 || mysize.height > 13 defines the size of the image which is hardcoded there. you should change it to suit your needs.
Note that this is an ASP question. It's worth to mention that you need a reference to Windows.Forms.Drawing, plus that it's in general not recommended practice to include it in ASP projects.
The title says C#, sorry. And the reference is System.Drawing. It is worth using in web applications such as ASP.net. I know it because I use it like I use GD in PHP.
1

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.

Comments

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.