C#中添加字符串形象,用最大的字体大小 [英] C# adding string to image, using the max font size

查看:108
本文介绍了C#中添加字符串形象,用最大的字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要2字符串添加到图片如下:

 这是文本
------------
------------
------------
- 其他 - - -

我如何使用的最大字体大小可能不会有串熄灭图像的一面呢?

例如:如果文本太大然后熄灭图像:

 这是文本太大
------------
------------
------------
- 其他 - - -


解决方案

我写了一篇关于我的previous项目这个脚本通过计算它的尺寸为每个字体大小以适合一些文字到图像。当字体大小大于图象的宽度更大,它由0.1em降低的字体大小,并再次尝试直到该文本在图像中适合。这里的code:

 公共静态字符串drawTextOnMarker(字符串markerfile,字符串文本,串newfilename,彩色文字颜色)
    {
        //乌里URI =新的URI(markerfile,UriKind.Relative);
        // markerfile = uri.AbsolutePath;
        // URI =新的URI(newfilename,UriKind.Relative);
        // newfilename = uri.AbsolutePath;
        如果(!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(newfilename)))
        {
            尝试
            {                BMP位图=新位图(System.Web.HttpContext.Current.Server.MapPath(markerfile));
                图形G = Graphics.FromImage(BMP);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias​​GridFit;
                的StringFormat strFormat =新的StringFormat();
                strFormat.Alignment = StringAlignment.Center;
                SolidBrush myBrush =新SolidBrush(文字颜色);
                浮字号= 10;
                布尔sizeSet​​upCompleted = FALSE;
                而(!sizeSet​​upCompleted)
                {
                    的SizeF mySize = g.MeasureString(文字,新字体(宋体,字号,FontStyle.Bold));
                    如果(mySize.Width> 24 || mySize.Height> 13)
                    {
                        fontsize- = float.Parse(0.1);
                    }
                    其他
                    {
                        sizeSet​​upCompleted = TRUE;
                    }
                }                g.DrawString(文字,新字体(宋体,字号,FontStyle.Bold),myBrush,新的RectangleF(4,3,24,8),strFormat);
                bmp.Save(System.Web.HttpContext.Current.Server.MapPath(newfilename));
                返回newfilename.Substring(2);
            }
            赶上(例外)
            {
                返回markerfile.Substring(2);
            }
        }
        返回newfilename.Substring(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-----

解决方案

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);
    }

这篇关于C#中添加字符串形象,用最大的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆