开发者

Drawing text in .NET

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

Drawing text in .NET

  • The first string is a native Label with the FlatStyle set to System
  • The second string is drawn using Graphics.DrawString()开发者_StackOverflow 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?


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);
    }
}


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜