You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何C#小字号图形文本抗锯齿质量差?如何解决?

C#中Bebas字体小字号文本渲染质量问题

我尝试在C#中使用Bebas字体将文本渲染到Bitmap上,以下是从文件系统加载.png模板并叠加文本的核心代码:

public Bitmap CreateCard(AccumulatedStats player, string template, bool includeGamerpic = false)
{
    Bitmap imageCache = new Bitmap(template);
    using (Graphics g = Graphics.FromImage(imageCache))
    {
        //AddAvatar(g, player);
        AddGamertag(g, player);
        //AddStats(g, player);
        //AddLogo(g, player);
        AddPrimaryPosition(g, player);
    }
    return imageCache;
}

其中AddGamertag方法绘制38号字体的文本,渲染效果正常:

效果良好的文本:大字号Bebas字体边缘平滑,显示清晰

但AddPrimaryPosition方法绘制12号字体的文本时,质量很差:

private void AddPrimaryPosition(Graphics g, AccumulatedStats player)
{
    Brush bgText = new SolidBrush(Color.FromArgb(red: 238, green: 243, blue: 234));
    var centerAlign = new StringFormat() { Alignment = StringAlignment.Center };
    Font bebas = new Font("Bebas", 12);

    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
    g.DrawString($"Lorem Ipsum\nYour Name Here", bebas, bgText, new PointF(601, 221), centerAlign);
    //g.DrawString($"Lorem Ipsum\nYour Name Here", bebas, Brushes.Black, new PointF(843, 221), centerAlign);
}

效果糟糕的文本:小字号Bebas字体边缘锯齿严重,模糊不清

注意我已经设置了SmoothingMode和TextRenderingHint开启抗锯齿,但问题依然存在。

对比Paint.NET的渲染质量:

Paint.NET设置:使用Bebas 12号字体,开启抗锯齿
Paint.NET渲染结果:文本边缘平滑,显示清晰

我找到一个类似问题的帖子,其中提到问题可能是区域未初始化。为验证这个猜想,我尝试先绘制彩色矩形作为背景,再绘制文本,期望抗锯齿能采样背景色:

private void AddPrimaryPosition(Graphics g, AccumulatedStats player)
{
    Brush bgText = new SolidBrush(Color.FromArgb(red: 238, green: 243, blue: 234));
    var centerAlign = new StringFormat() { Alignment = StringAlignment.Center };
    Font bebas = new Font("Bebas", 12);

    // 测试用红色偏移,实际应为0
    var islesBlue = new SolidBrush(Color.FromArgb(red: 200, green: 83, blue: 155));
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    Rectangle rect = new Rectangle(499, 219, 203, 66);
    g.FillRectangle(islesBlue, rect);
    g.DrawRectangle(new Pen(islesBlue), rect);
    

    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
    g.DrawString($"Lorem Ipsum\nYour Name Here", bebas, bgText, new PointF(601, 221), centerAlign);
    g.DrawString($"Lorem Ipsum\nYour Name Here", bebas, Brushes.Black, new PointF(843, 221), centerAlign);
}

仍糟糕的文本:即使提前绘制背景矩形,小字号文本的渲染质量依然没有改善

我还在右侧(和AddGamertag大字号文本相同背景色的区域)绘制了文本,结果同样糟糕。

我尝试将SmoothingMode设为None、TextRenderingHint设为SingleBitPerPixel,效果有变化(证明之前确实有抗锯齿生效),但质量还是很差:

无抗锯齿的糟糕文本:文本边缘锯齿明显,没有平滑过渡

问题

为何小字号下Bebas字体的抗锯齿质量如此糟糕?是否有无需增大字号或更换字体的解决方法?


内容的提问来源于stack exchange,提问作者ravibhagw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 09:07:40