首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本生成图像

从文本生成图像
EN

Stack Overflow用户
提问于 2021-07-15 21:12:42
回答 1查看 40关注 0票数 0

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

代码语言:javascript
复制
/// <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只是印刷和排版行业的虚拟文本。自15世纪以来,Lorem Ipsum一直是业界的标准虚拟文本,当时一家不知名的印刷商拿出了一个排版,然后把它弄乱了,做成了一个排版样本簿。

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

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-16 21:00:57

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

代码语言:javascript
复制
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;可显著提高质量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68394519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档