使用Graphics.DrawString时动态调整字体大小以适应空间

19

有没有什么提示可以动态调整字体大小以适应特定区域?例如,我有一个800x110矩形,我想用最大的字体填充它,以支持我要显示的整个字符串。

Bitmap bitmap = new Bitmap(800, 110);

using (Graphics graphics = Graphics.FromImage(bitmap))
using (Font font1 = new Font("Arial", 120, FontStyle.Regular, GraphicsUnit.Pixel))
{
    Rectangle rect1 = new Rectangle(0, 0, 800, 110);

    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.LineAlignment = StringAlignment.Center;

    graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
    graphics.DrawString("Billy Reallylonglastnameinstein", font1, Brushes.Red, rect1, stringFormat);
} 

bitmap.Save(Server.MapPath("~/Fonts/" + System.Guid.NewGuid() + ".png"));

很明显,在大字体大小提供的空间中不可能完整显示整个名称。有没有简单的方法可以解决这个问题?


2
也许可以使用TextRenderer.MeasureTextGraphics.MeasureString来测量文本大小,并迭代地降低字体大小直到适合为止? - Uwe Keim
@farina,我改变了我的回答,我认为这个解决方案可以帮助你解决它。 - saeed
5个回答

28

您应该对Font.Size进行比例转换。以下函数是进行此操作的示例,但您可以改进它以获得更好的结果。

这里是FindFont函数,它接收一个房间和一个具有首选大小的文本,并提供一个字体,您可以设置整个文本适合房间!

// This function checks the room size and your text and appropriate font
//  for your text to fit in room
// PreferedFont is the Font that you wish to apply
// Room is your space in which your text should be in.
// LongString is the string which it's bounds is more than room bounds.
private Font FindFont(
   System.Drawing.Graphics g,
   string longString,
   Size Room,
   Font PreferedFont
) {
   // you should perform some scale functions!!!
   SizeF RealSize = g.MeasureString(longString, PreferedFont);
   float HeightScaleRatio = Room.Height / RealSize.Height;
   float WidthScaleRatio = Room.Width / RealSize.Width;

   float ScaleRatio = (HeightScaleRatio < WidthScaleRatio)
      ? ScaleRatio = HeightScaleRatio
      : ScaleRatio = WidthScaleRatio;

   float ScaleFontSize = PreferedFont.Size * ScaleRatio;

   return new Font(PreferedFont.FontFamily, ScaleFontSize);
}

对于您的问题,您可以像下面的代码一样进行调用:

Bitmap bitmap = new Bitmap(800, 110);

using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
using (Font font1 = new Font("Arial", 120, FontStyle.Regular, GraphicsUnit.Pixel))
{
   Rectangle rect1 = new Rectangle(0, 0, 800, 110);

   StringFormat stringFormat = new StringFormat();
   stringFormat.Alignment = StringAlignment.Center;
   stringFormat.LineAlignment = StringAlignment.Center;
   graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

   Font goodFont = FindFont(graphics, "Billy Reallylonglastnameinstein", rect1.Size, font1);

   graphics.DrawString(
      "Billy Reallylonglastnameinstein",
      goodFont,
      Brushes.Red,
      rect1,
      stringFormat
   );
}

2
float ScaleRatio = Math.Min(HeightScaleRatio, WidthScaleRatio); 这句话怎么翻译? - gilly3
1
这可能会接近,但似乎文本换行可能会造成不准确性。 对于单行文本来说,这是一个不错的方法。 - gilly3
还有很多其他的选择可以做...比如文本处理,甚至是文本图像算法之类的,这取决于程序员的想法。根据我的帖子,这是一个简单明了的解决方案!只是一个改进的基础想法!!! - saeed
我在我的组件中使用了这个:http://stackoverflow.com/questions/25891547/draw-character-in-center-of-custom-control-font-awesome-glyph 谢谢分享! - Misiu
1
FindFont()函数的返回行中添加GraphicsUnit.Pixel。如果没有GraphicsUnit.Pixel,系统dpi将影响绘制的字符串。这是该行的样子:return new Font(PreferedFont.FontFamily, ScaleFontSize, PreferedFont.Style, GraphicsUnit.Pixel); - Rijul Sudhir
显示剩余2条评论

4
这只是对@Saeed的FindFont函数的更新。
需要在FindFont函数的返回行中添加GraphicsUnit.Pixel。没有GraphicsUnit.Pixel,系统dpi将影响绘制的字符串。当系统和位图的dpi不匹配时,问题会出现。您可以在Windows DPI setting affects Graphics.DrawString中了解更多详细信息。由于PreferedFontGraphicsUnit已经设置为GraphicsUnit.Pixel并且返回的字体未设置为GraphicsUnit.Pixel。在这种情况下,如果位图dpi大于系统dpi,则文本将超出Room维度,如果位图dpi小于系统dpi,则字体大小将变小,而不是预期的大小。这是更新后的函数。
    private Font FindFont(  System.Drawing.Graphics g , string longString , Size Room , Font PreferedFont)
    {
        SizeF RealSize = g.MeasureString(longString, PreferedFont);
        float HeightScaleRatio = Room.Height / RealSize.Height;
        float WidthScaleRatio = Room.Width / RealSize.Width;
        float ScaleRatio = (HeightScaleRatio < WidthScaleRatio) ? ScaleRatio = HeightScaleRatio : ScaleRatio = WidthScaleRatio;
        float ScaleFontSize = PreferedFont.Size * ScaleRatio;
        return new Font(PreferedFont.FontFamily, ScaleFontSize,PreferedFont.Style,GraphicsUnit.Pixel);
    }

这很有趣。我从未遇到过这个问题,但我相信这是可能的。 - farina

4

我已经根据我的需求调整了Saeed的伟大函数。注释解释所有内容:

    // You hand this the text that you need to fit inside some
    // available room, and the font you'd like to use.
    // If the text fits nothing changes
    // If the text does not fit then it is reduced in size to
    // make it fit.
    // PreferedFont is the Font that you wish to apply
    // FontUnit is there because the default font unit is not
    // always the one you use, and it is info required in the
    // constructor for the new Font.
    public static void FindGoodFont(Graphics Graf, string sStringToFit,
                                    Size TextRoomAvail, 
                                    ref Font FontToUse,
                                    GraphicsUnit FontUnit)
    {
        // Find out what the current size of the string in this font is
        SizeF RealSize = Graf.MeasureString(sStringToFit, FontToUse);
        Debug.WriteLine("big string is {0}, orig size = {1},{2}",
                         sStringToFit, RealSize.Width, RealSize.Height);
        if ((RealSize.Width <= TextRoomAvail.Width) && (RealSize.Height <= TextRoomAvail.Height))
        {
            Debug.WriteLine("The space is big enough already");
            // The current font is fine...
            return;
        }

        // Either width or height is too big...
        // Usually either the height ratio or the width ratio
        // will be less than 1. Work them out...
        float HeightScaleRatio = TextRoomAvail.Height / RealSize.Height;
        float WidthScaleRatio = TextRoomAvail.Width / RealSize.Width;

        // We'll scale the font by the one which is furthest out of range...
        float ScaleRatio = (HeightScaleRatio < WidthScaleRatio) ? ScaleRatio = HeightScaleRatio : ScaleRatio = WidthScaleRatio;
        float ScaleFontSize = FontToUse.Size * ScaleRatio;

        Debug.WriteLine("Resizing with scales {0},{1} chose {2}",
                         HeightScaleRatio, WidthScaleRatio, ScaleRatio);

        Debug.WriteLine("Old font size was {0}, new={1} ",FontToUse.Size,ScaleFontSize);

        // Retain whatever the style was in the old font...
        FontStyle OldFontStyle = FontToUse.Style;

        // Get rid of the old non working font...
        FontToUse.Dispose();

        // Tell the caller to use this newer smaller font.
        FontToUse = new Font(FontToUse.FontFamily,
                                ScaleFontSize,
                                OldFontStyle,
                                FontUnit);
    }

2

我不想抨击saaeds的解决方案,它可能也非常棒。但是我在msdn上找到了另一个解决方案:动态图形文本调整大小,这对我很有用。

public Font GetAdjustedFont(Graphics GraphicRef, string GraphicString, Font OriginalFont, int ContainerWidth, int MaxFontSize, int MinFontSize, bool SmallestOnFail)
{
   // We utilize MeasureString which we get via a control instance           
   for (int AdjustedSize = MaxFontSize; AdjustedSize >= MinFontSize; AdjustedSize--)
   {
      Font TestFont = new Font(OriginalFont.Name, AdjustedSize, OriginalFont.Style);

      // Test the string with the new size
      SizeF AdjustedSizeNew = GraphicRef.MeasureString(GraphicString, TestFont);

      if (ContainerWidth > Convert.ToInt32(AdjustedSizeNew.Width))
      {
       // Good font, return it
         return TestFont;
      }
   }

   // If you get here there was no fontsize that worked
   // return MinimumSize or Original?
   if (SmallestOnFail)
   {
      return new Font(OriginalFont.Name,MinFontSize,OriginalFont.Style);
   }
   else
   {
      return OriginalFont;
   }
}

我也觉得这篇博客文章(http://www.hanselman.com/blog/NuGetPackageOfTheWeekImageProcessorLightweightImageManipulationInC.aspx)非常有用。当我问这个问题的时候,saeed的解决方案非常完美,而且我认为所有这些解决方案都非常准确。 - farina

1
这是我的解决方案,支持换行。
public static Font GetAdjustedFont(Graphics graphic, string str, Font originalFont, Size containerSize)
    {
        // We utilize MeasureString which we get via a control instance           
        for (int adjustedSize = (int)originalFont.Size; adjustedSize >= 1; adjustedSize--)
        {
            var testFont = new Font(originalFont.Name, adjustedSize, originalFont.Style, GraphicsUnit.Pixel);

            // Test the string with the new size
            var adjustedSizeNew = graphic.MeasureString(str, testFont, containerSize.Width);

            if (containerSize.Height > Convert.ToInt32(adjustedSizeNew.Height))
            {
                // Good font, return it
                return testFont;
            }
        }

        return new Font(originalFont.Name, 1, originalFont.Style, GraphicsUnit.Pixel);
    }

如何使用:

var font = GetAdjustedFont(drawing, text, originalfont, wrapSize);
drawing.DrawString(text, font, textBrush, new Rectangle(0, 0, wrapSize.Width, wrapSize.Height));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接