【问题标题】:Generate image from text从文本生成图像
【发布时间】:2024-05-04 19:45:03
【问题描述】:

我想从文本生成图像,所以我从以前的问题中找到了一个代码,如下所示

/// <summary>
/// Creates an image containing the given text.
/// NOTE: the image should be disposed after use.
/// </summary>
/// <param name="text">Text to draw</param>
/// <param name="fontOptional">Font to use, defaults to Control.DefaultFont</param>
/// <param name="textColorOptional">Text color, defaults to Black</param>
/// <param name="backColorOptional">Background color, defaults to white</param>
/// <param name="minSizeOptional">Minimum image size, defaults the size required to display the text</param>
/// <returns>The image containing the text, which should be disposed after use</returns>
public static Image DrawText(string text, Font fontOptional=null, Color? textColorOptional=null, Color? backColorOptional=null, Size? minSizeOptional=null)
{
    Font font = Control.DefaultFont;
    if (fontOptional != null)
        font = fontOptional;

    Color textColor = Color.Black;
    if (textColorOptional != null)
        textColor = (Color)textColorOptional;

    Color backColor = Color.White;
    if (backColorOptional != null)
        backColor = (Color)backColorOptional;

    Size minSize = Size.Empty;
    if (minSizeOptional != null)
        minSize = (Size)minSizeOptional;

    //first, create a dummy bitmap just to get a graphics object
    SizeF textSize;
    using (Image img = new Bitmap(1, 1))
    {
        using (Graphics drawing = Graphics.FromImage(img))
        {
            //measure the string to see how big the image needs to be
            textSize = drawing.MeasureString(text, font);
            if (!minSize.IsEmpty)
            {
                textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width;
                textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height;
            }
        }
    }

    //create a new image of the right size
    Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height);
    using (var drawing = Graphics.FromImage(retImg))
    {
        //paint the background
        drawing.Clear(backColor);

        //create a brush for the text
        using (Brush textBrush = new SolidBrush(textColor))
        {
            drawing.DrawString(text, font, textBrush, 0, 0);
            drawing.Save();
        }
    }
    return retImg;
}

这段代码运行良好。它还处理您要创建的最小尺寸。

现在我还想处理要处理的图像的最大宽度,例如如果我将图像的 maxWidth 传递为 30 个字符,那么它将根据其宽度创建少于 30 个字符的图像,但如果任何行有更多超过 30 个字符,则 30 个以上的字符应先出现在下一行。

例如-如果我需要创建 maxWidth 50 个字符的以下数据的图像:

Lorem Ipsum 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商使用了一种类型的厨房并加扰它来制作类型样本书。

然后它应该创建如下图像:

Lorem Ipsum is simply dummy text of the printing a
nd typesetting industry. Lorem Ipsum has been the 
industry's standard dummy text ever since the 1500
s, when an unknown printer took a galley of type a
nd scrambled it to make a type specimen book.

【问题讨论】:

  • 我不确定我是否理解您想要达到的目标。您是否只想从字符串中获取前 X 个字符?如果是这样,请查看Substring 方法。例如var newString = originalString.Substring(0, 50);

标签: c# image drawing


【解决方案1】:

MeasureStringDrawString 方法具有重载,允许您指定文本的大小。这些方法会根据给定的宽度自动换行。

drawing.TextRenderingHint = TextRenderingHint.AntiAlias; // Improves quality.
var rect = new RectangleF(0, 0, width, height); // Specify maximum width and height.
SizeF textSize = drawing.MeasureString(text, font, rect.Size);
// Use textSize to do calculations.
drawing.DrawString(text, font, textBrush, rect);

MesaureString 的另一个重载允许您仅指定 with。这两种方法都具有带有StringFormat 参数的重载,从而可以更好地控制间距和对齐方式。

设置drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; 可显着提高质量。

【讨论】: