在.NET中绘制文本文本、NET

2023-09-02 01:41:55 作者:娜样杰出的星光

我做了一些测试,有关.NET中绘制文本,我有以下结果。

I'm doing some tests about drawing text in .Net and I had the following results.

在第一个字符串是一个本地标签的的FlatStyle 设置为系统 第二个字符串使用绘制Graphics.DrawString()方法 最后一个是用画TextRenderer.DrawText()方法 The first string is a native Label with the FlatStyle set to System The second string is drawn using Graphics.DrawString() method The last one is drawn using TextRenderer.DrawText() method

所有情况下使用默认的Windows Vista / 7的字体:的Segoe UI,9

All cases use the default Windows Vista/7 font: Segoe UI, 9

正如你所看到的,是第二个字符串,其他(有更少的质量和抗别名是不同的)之间的差异。我试图配置抗混叠和图形对象平滑模式,没有任何结果。

As you can see, there is a difference between the second string and the others (it has less quality, and the anti alias is different). I have tried to configure anti-alias and the smoothing mode in the Graphics object, without any result.

是否有可能绘制文本usign Graphics.DrawString ,并得到同样的质量比其他的方法呢?

Is it possible to draw text usign Graphics.DrawString and get the same quality than others methods?

在此先感谢。

编辑:我已经查阅了code与反射。我意识到, Graphics.DrawString 使用的gdiplus.dll 调用方法GdipDrawString()和 TextRenderer.DrawText 使用 user32.dll中调用 DrawTextExW DrawTextExA

I have reviewed the code with Reflector. I realized that Graphics.DrawString uses gdiplus.dll calling method GdipDrawString() and TextRenderer.DrawText uses user32.dll calling DrawTextExW and DrawTextExA.

这件事有何评论?

推荐答案

GDI +是在渲染分辨率独立的文本微软的第一次尝试。而唯一的方式呈现在.NET 1.x中的文本它得到了广泛的淘过质量问题,启发在.NET 2.0中引入TextRenderer和Application.SetCompatibleTextRenderingDefault()的。它采用GDI绘制文本,有效地解决这些问题。你只应该在高分辨率设备上使用Graphics.DrawString()。打印机。

GDI+ was Microsoft's first attempt at rendering resolution independent text. And the only way to render text in .NET 1.x. It got widely panned for its quality issues, inspiring the introduction of TextRenderer and Application.SetCompatibleTextRenderingDefault() in .NET 2.0. It uses GDI for drawing text, effectively solving the problems. You should only use Graphics.DrawString() on high resolution devices. Printers.

FWIW,第二次尝试是WPF,同时也得到了很多宣传的模糊的文字问题。解决了在.NET 4。

Fwiw, the second attempt was WPF and it also got a lot of flack for fuzzy text problems. Solved in .NET 4.

试试这个示例的形式看到的最严重的问题之一:

Try this sample form to see one of the worst problems:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.DrawString("Hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", 
            this.Font, Brushes.Black, 0, 0);
    }
}