在 .NET 中绘制文本
我正在做一些关于在 .Net 中绘制文本的测试,我得到了以下结果。
- 第一个字符串是本机标签,其
FlatStyle
设置为System
- 第二个字符串是使用
Graphics.DrawString()
方法 - 最后一个是使用
TextRenderer.DrawText()
方法
所有情况都使用默认的 Windows Vista/7 字体:Segoe UI, 9
如您所见,有一个之间的差异第二个字符串和其他字符串(它的质量较差,并且抗锯齿不同)。我尝试在 Graphics 对象中配置抗锯齿和平滑模式,但没有任何结果。
是否可以使用 Graphics.DrawString 绘制文本并获得与其他方法相同的质量?
提前致谢。
编辑:我已经使用 Reflector 检查了代码。我意识到 Graphics.DrawString
使用 gdiplus.dll 调用方法 GdipDrawString() 和 TextRenderer.DrawText
使用 user32.dll > 调用 DrawTextExW
和 DrawTextExA
。
对此有何评论?
I'm doing some tests about drawing text in .Net and I had the following results.
- The first string is a native Label with the
FlatStyle
set toSystem
- The second string is drawn using
Graphics.DrawString()
method - The last one is drawn using
TextRenderer.DrawText()
method
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
GDI+ 是微软首次尝试渲染与分辨率无关的文本。这是在 .NET 1.x 中渲染文本的唯一方法。它因其质量问题而受到广泛批评,并启发了在 .NET 2.0 中引入 TextRenderer 和 Application.SetCompatibleTextRenderingDefault()。它使用GDI来绘制文本,有效地解决了这个问题。您应该只在高分辨率设备上使用 Graphics.DrawString()。打印机。
Fwiw,第二次尝试是 WPF,它也因模糊文本问题而受到很多批评。在 .NET 4 中已解决。
尝试使用此示例表单来查看最严重的问题之一:
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:
以下代码来自 MSDN 上的示例:
我对此进行了测试,效果很好,在我的表单上绘制了平滑的文本! ;) 这是文章的链接。
Following code comes from an example on MSDN:
I tested this and it worked fine, a smooth text was drawn on my form! ;) Here's the link to the article.
Graphics.DrawString
方法位于System.Drawing
命名空间,这意味着它在幕后使用 GDI+,而不是TextRenderer.DrawText
方法所使用的 GDI。看起来这种情况下差异的具体原因是抗锯齿。您可以通过
图形控制抗锯齿。 TextRenderingHint
属性。我相信您也可以使用 Abbas 所示的方法在每个字体的基础上禁用它。
The
Graphics.DrawString
method resides in theSystem.Drawing
namespace, which means it uses GDI+ under the covers instead of GDI, which is what theTextRenderer.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.I believe that you can also disable it on a per-font basis using the method shown by Abbas.
除了其他建议(在您的情况下可能更正确)之外,您还可以尝试使用基于八叉树的图像量化。
我用它来处理普通图片,而不是文本。
有一篇微软文章详细讨论了这一点,并有一个示例项目:
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