首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在运行时从动态文本生成图像

如何在运行时从动态文本生成图像
EN

Stack Overflow用户
提问于 2010-01-15 16:48:29
回答 5查看 99.5K关注 0票数 67

谁可以指导如何从输入文本生成图像。图像可能有任何扩展名都无关紧要。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-01-15 17:19:06

好的,假设您想要在C#中的图像上绘制一个字符串,您需要在这里使用System.Drawing命名空间:

代码语言:javascript
复制
private Image DrawText(String text, Font font, Color textColor, Color backColor)
{
    //first, create a dummy bitmap just to get a graphics object
    Image img = new Bitmap(1, 1);
    Graphics drawing = Graphics.FromImage(img);

    //measure the string to see how big the image needs to be
    SizeF textSize = drawing.MeasureString(text, font);

    //free up the dummy image and old graphics object
    img.Dispose();
    drawing.Dispose();

    //create a new image of the right size
    img = new Bitmap((int) textSize.Width, (int)textSize.Height);

    drawing = Graphics.FromImage(img);

    //paint the background
    drawing.Clear(backColor);

    //create a brush for the text
    Brush textBrush = new SolidBrush(textColor);

    drawing.DrawString(text, font, textBrush, 0, 0);

    drawing.Save();

    textBrush.Dispose();
    drawing.Dispose();

    return img;

}

这段代码将首先测量字符串,然后创建正确大小的图像。

如果您想保存此函数的返回结果,只需调用返回图像的save方法即可。

票数 173
EN

Stack Overflow用户

发布于 2016-06-27 11:49:46

谢谢,卡扎。对之前的答案稍有改进,用于在使用后处理图像/图形对象,并引入了最小尺寸参数

代码语言:javascript
复制
    private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor) {
        return DrawTextImage(currencyCode, font, textColor, backColor, Size.Empty);
    }
    private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor, Size minSize) {
        //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(currencyCode, 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(currencyCode, font, textBrush, 0, 0);
                drawing.Save();
            }
        }
        return retImg;
    }
票数 10
EN

Stack Overflow用户

发布于 2019-07-27 00:17:15

这是Panayiotis版本的Kazar的答案,带有可选的参数和文档,适合添加到库类中。

代码语言: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;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2070365

复制
相关文章

相似问题

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