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

显然,整个名称不会在大字体提供的空间中呈现。必须有一个简单的方法来做到这一点?

4

5 回答 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
   );
}
于 2013-10-30T06:01:31.320 回答
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);
    }
于 2014-05-26T07:16:53.120 回答
3

这只是@SaeedFindFont功能的更新。

GraphicsUnit.Pixel需要添加到FindFont函数返回行。没有GraphicsUnit.Pixel,系统 dpi 将影响绘制的字符串。当系统和位图的 dpi 不匹配时,就会出现问题。您可以在此Windows DPI 设置影响 Graphics.DrawString中查看更多详细信息。由于 GraphicsUnitPreferedFont已设置为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);
    }
于 2017-11-09T22:11:51.170 回答
2

我不想抨击 saaeds 解决方案,它也可能非常棒。但我在 msdn 上找到了另一个:Dynamic Graphic Text Resizing,它对我有用。

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;
   }
}
于 2014-06-18T12:16:11.127 回答
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));
于 2018-03-31T09:46:21.397 回答