在 .NET 中绘制文本

发布于 2024-12-02 10:48:00 字数 1005 浏览 3 评论 0原文

我正在做一些关于在 .Net 中绘制文本的测试,我得到了以下结果。

绘图文本示例

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

如您所见,有一个之间的差异第二个字符串和其他字符串(它的质量较差,并且抗锯齿不同)。我尝试在 Graphics 对象中配置抗锯齿和平滑模式,但没有任何结果。

是否可以使用 Graphics.DrawString 绘制文本并获得与其他方法相同的质量?

提前致谢。


编辑:我已经使用 Reflector 检查了代码。我意识到 Graphics.DrawString 使用 gdiplus.dll 调用方法 GdipDrawString() 和 TextRenderer.DrawText 使用 user32.dll > 调用 DrawTextExWDrawTextExA

对此有何评论?

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

Drawing text example

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.

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

Thanks in advance.


EDIT: 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.

Any comment about it?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

oО清风挽发oО 2024-12-09 10:48:00

GDI+ 是微软首次尝试渲染与分辨率无关的文本。这是在 .NET 1.x 中渲染文本的唯一方法。它因其质量问题而受到广泛批评,并启发了在 .NET 2.0 中引入 TextRenderer 和 Application.SetCompatibleTextRenderingDefault()。它使用GDI来绘制文本,有效地解决了这个问题。您应该只在高分辨率设备上使用 Graphics.DrawString()。打印机。

Fwiw,第二次尝试是 WPF,它也因模糊文本问题而受到很多批评。在 .NET 4 中已解决。

尝试使用此示例表单来查看最严重的问题之一:

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

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, 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);
    }
}
穿越时光隧道 2024-12-09 10:48:00

以下代码来自 MSDN 上的示例:

var fontFamily = new FontFamily("Times New Roman");
var font = new Font(fontFamily, 32, FontStyle.Regular, GraphicsUnit.Pixel);
var solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString("Your Text Here", font, solidBrush, new PointF(10, 60));

我对此进行了测试,效果很好,在我的表单上绘制了平滑的文本! ;) 这是文章的链接。

Following code comes from an example on MSDN:

var fontFamily = new FontFamily("Times New Roman");
var font = new Font(fontFamily, 32, FontStyle.Regular, GraphicsUnit.Pixel);
var solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString("Your Text Here", font, solidBrush, new PointF(10, 60));

I tested this and it worked fine, a smooth text was drawn on my form! ;) Here's the link to the article.

咆哮 2024-12-09 10:48:00

Graphics.DrawString 方法位于 System.Drawing 命名空间,这意味着它在幕后使用 GDI+,而不是 TextRenderer.DrawText 方法所使用的 GDI。

看起来这种情况下差异的具体原因是抗锯齿。您可以通过图形控制抗锯齿。 TextRenderingHint 属性。

graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

我相信您也可以使用 Abbas 所示的方法在每个字体的基础上禁用它。

The Graphics.DrawString method resides in the System.Drawing namespace, which means it uses GDI+ under the covers instead of GDI, which is what the TextRenderer.DrawText method is using.

It looks like the specific cause of the difference in this case is anti-aliasing. You can control anti-aliasing through the Graphics.TextRenderingHint property.

graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

I believe that you can also disable it on a per-font basis using the method shown by Abbas.

时常饿 2024-12-09 10:48:00

除了其他建议(在您的情况下可能更正确)之外,您还可以尝试使用基于八叉树的图像量化。
我用它来处理普通图片,而不是文本。
有一篇微软文章详细讨论了这一点,并有一个示例项目:
http://msdn.microsoft.com/en-us/library/aa479306.aspx

Apart from the other suggestions, which are probably more correct in your case, you could also try to use an Octree-based Quantization of the image.
I use it for normal pictures, not for text.
There is a Microsoft article that talks in details about this and has an example project:
http://msdn.microsoft.com/en-us/library/aa479306.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文