【问题标题】:Antialiased text is not antialiased and Cleartype text is very ugly in windows forms抗锯齿文本没有抗锯齿,并且 Cleartype 文本在 Windows 窗体中非常难看
【发布时间】:2015-06-15 19:47:01
【问题描述】:

我有以下 C# 代码从 Windows 7 64 位上的位图中获取图形

protected Graphics GetImageGraphics()
{
    var g = Graphics.FromImage(image);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    g.FillRectangle(backBrush, new Rectangle(Point.Empty, Bounds.Size));
    return g;
}

这个用法如下:

 protected override void Redraw()
 {
    var g = GetImageGraphics();

    var path = RoundedRectanglePath(shapeRect, CornerRadius);
    g.FillPath(selected ? selectedBrush : brush, path);
    g.DrawPath(pen, path);

    foreach(var pin in pins)
        pin.Value.Render(g);

    var labelMax = shapeRect;
    labelMax.Inflate(-10, -10);

    var fmt = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak | TextFormatFlags.WordEllipsis;
    TextRenderer.DrawText(g, Task.Symbol.Label, Font, labelMax, Color.Black, fmt);

    fmt = TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis;
    TextRenderer.DrawText(g, Task.TaskId, Font, idRect, Color.Black, fmt);
}

然后使用以下命令将创建的图像复制到屏幕:

g.DrawImageUnscaled(image, Bounds);

生成的文本非常难看。它呈现为纯黑色,其中每个像素已被三个相邻的水平像素替换(就像旧的点阵打印机粗体字体)

如果我将 TextRenderingHint 更改为 AntiAlias 或 AntiAliasGridFit,则字体会更清晰地呈现,但没有任何抗锯齿(如 SingleBitPerPixel)

我做错了什么?

【问题讨论】:

  • 您必须绘制背景才能进行抗锯齿工作。例如添加 g.Clear(Color.White)。

标签: c# windows winforms


【解决方案1】:

解决方案有两个。正如 Hans 所指出的,文本不会抗锯齿变成透明的。答案的另一部分是不要在内存位图上使用 TextRenderer,如此处所述https://stackoverflow.com/a/1578056/429328

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-22
    • 2013-07-25
    • 2014-11-16
    • 2014-02-18
    • 1970-01-01
    • 2011-02-21
    • 2014-07-25
    • 1970-01-01
    相关资源
    最近更新 更多