从文本生成图像

Generate image from text

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

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

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

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

例如-如果我需要创建最大宽度为 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.

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

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; 显着提高质量。