18

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?

Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
  • 1
    do you use exactly same font in all cases ? – Tigran Sep 01 '11 at 09:18
  • You are just missing the `AntiAlias` option... – balexandre Sep 01 '11 at 09:41
  • Had this same problem on Windows XP. Having ClearType turned on was causing the problem. (You lose the "edges", so the font edge becomes "blocky". I do not know if you can turn ClearType off in Windows 7. – AMissico Sep 01 '11 at 09:46
  • I used: //'Only anti-alias works when user enabled clear-type. `g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit` – AMissico Sep 01 '11 at 09:50

4 Answers4

15

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.

Abbas
  • 14,186
  • 6
  • 41
  • 72
  • Sorry, but this answer is not correct. Please, try to reproduce the example by using a system label with the same that font and color, and you will see that the text is not exactly drawn. -1. – Daniel Peñalba Sep 01 '11 at 10:18
  • 2
    Ok, there is a MINOR difference but if you use "TextRenderingHint.AntiAliasGridFit", the quality is equal. I reproduced my example with a Label to check the results! :) – Abbas Sep 01 '11 at 17:34
15

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);
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

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.

Justin
  • 84,773
  • 49
  • 224
  • 367
1

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

Durden81
  • 966
  • 9
  • 25