如何将 Unicode 文本转换为位图

发布于 2025-01-18 03:01:10 字数 2889 浏览 0 评论 0 原文

我需要将包含Unicode Chars的文本转换为可能具有透明背景的位图。 我找到并尝试了不同的帖子,例如 this -trom-string to-inimage-in-c-in-c-sharp“> this ,但似乎没有人对我有用。 我还发现这篇文章建议使用 textrenderer.drawtext()方法代替 System.Drawing.graphics.drawString(),但最终结果还是不好的。

那是一个代码段:

private static Bitmap ImageFromText(string text, Font font, Color textColor, Color fillColor, System.Drawing.Graphics graphics)
{
    Bitmap bmpOut;

    SizeF sz = TextRenderer.MeasureText(graphics, text, font);
    //SizeF sz = g.MeasureString(text, font);

    if (sz.IsEmpty)

        sz = new SizeF(1, 1);

    bmpOut = new Bitmap((int) Math.Ceiling(sz.Width), (int) Math.Ceiling(sz.Height), PixelFormat.Format32bppArgb);
    using (System.Drawing.Graphics gBmp = System.Drawing.Graphics.FromImage(bmpOut))
    {
        gBmp.Clear(fillColor);

        gBmp.SmoothingMode = SmoothingMode.HighQuality;
        gBmp.InterpolationMode = InterpolationMode.HighQualityBilinear;
        gBmp.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

        //gBmp.DrawString(text, font, brFore, 0, 0); // Unicode chars are not supported
        TextRenderer.DrawText(gBmp, text, font, new Point(0, 0), textColor);
    }

    return bmpOut;
}

这是我的测试代码:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();                
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        string testString = "Unicode Title \uD83E\uDC25";
        var font = new Font(new FontFamily("Microsoft Sans Serif"), 12.375f);

        // e.Graphics.DrawString(testString, new Font(font.FontFamily, 20), new SolidBrush(Color.Black), 0, 0);

        TextRenderer.DrawText(e.Graphics, testString, new Font ( font.FontFamily, 20 ), new Point(0, 50), Color.Black);

        Bitmap bitmap = ImageFromText(testString, new Font(font.FontFamily, 20), Color.Black, this.BackColor, e.Graphics);

        e.Graphics.DrawImage(bitmap, new Point(0, 100));                

        //Clipboard.SetImage(bitmap);

        
    }
}

这是糟糕的结果:

“在此处输入图像描述”

我如何改善位图结果?

edit

简短回顾:

  • 我无法使用system.drawing.graphics.drawString()方法,因为它不支持Unicode chars,
  • 所以我无法使用textrenderer.drawtext()方法,因为TexTrenderer类使用GDI和GDI和它不知道Alpha,因此不支持透明背景。

还有其他方法可以实现我的目标吗?

I need to convert a text containing Unicode chars into a Bitmap that could have a transparent background as well.
I found and tried different posts like this, this, or this, but no one seems to work for me.
I found also this post that suggests using TextRenderer.DrawText() method instead of System.Drawing.Graphics.DrawString() but the final result is not good anyway.

That's a code snippet:

private static Bitmap ImageFromText(string text, Font font, Color textColor, Color fillColor, System.Drawing.Graphics graphics)
{
    Bitmap bmpOut;

    SizeF sz = TextRenderer.MeasureText(graphics, text, font);
    //SizeF sz = g.MeasureString(text, font);

    if (sz.IsEmpty)

        sz = new SizeF(1, 1);

    bmpOut = new Bitmap((int) Math.Ceiling(sz.Width), (int) Math.Ceiling(sz.Height), PixelFormat.Format32bppArgb);
    using (System.Drawing.Graphics gBmp = System.Drawing.Graphics.FromImage(bmpOut))
    {
        gBmp.Clear(fillColor);

        gBmp.SmoothingMode = SmoothingMode.HighQuality;
        gBmp.InterpolationMode = InterpolationMode.HighQualityBilinear;
        gBmp.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

        //gBmp.DrawString(text, font, brFore, 0, 0); // Unicode chars are not supported
        TextRenderer.DrawText(gBmp, text, font, new Point(0, 0), textColor);
    }

    return bmpOut;
}

Here is my test code:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();                
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        string testString = "Unicode Title \uD83E\uDC25";
        var font = new Font(new FontFamily("Microsoft Sans Serif"), 12.375f);

        // e.Graphics.DrawString(testString, new Font(font.FontFamily, 20), new SolidBrush(Color.Black), 0, 0);

        TextRenderer.DrawText(e.Graphics, testString, new Font ( font.FontFamily, 20 ), new Point(0, 50), Color.Black);

        Bitmap bitmap = ImageFromText(testString, new Font(font.FontFamily, 20), Color.Black, this.BackColor, e.Graphics);

        e.Graphics.DrawImage(bitmap, new Point(0, 100));                

        //Clipboard.SetImage(bitmap);

        
    }
}

And this is the poor result:

enter image description here

How can I improve the bitmap result?

EDIT

Short recap:

  • I can't use System.Drawing.Graphics.DrawString() method because it does not support Unicode chars
  • I can't use TextRenderer.DrawText() method because TextRenderer class uses GDI and it isn't alpha aware, so it does not support a transparent background.

Is there some other way to reach my goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文