Drawing text in .NET

28,184

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Share:
28,184
Daniel Peñalba
Author by

Daniel Peñalba

Software Engineer at Unity Technologies, Valladolid, Spain. Currently developing gmaster, Plastic SCM and SemanticMerge. Areas: C# GUI development Winforms and WPF expert ASP .NET Core Multiplatform UI development with Mono (Linux and OSX, GTK# and MonoMac) Eclipse plugin, Java Automated testing, NUnit, Moq, PNUnit and TestComplete Email: dpenalba[AT]codicesoftware[DOT]com I play the guitar at Sharon Bates, the greatest Spanish rock band.

Updated on August 30, 2020

Comments

  • Daniel Peñalba
    Daniel Peñalba over 3 years

    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
    Daniel Peñalba over 12 years
    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
    Daniel Peñalba over 12 years
    Played with TextRenderingHint, tested all possibilities and no favorable result.
  • Abbas
    Abbas over 12 years
    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! :)