Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing text in .NET

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

Drawing text example

  • The first string is a native Label with the FlatStyle set to System
  • 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?

like image 383
Daniel Peñalba Avatar asked Sep 01 '11 09:09

Daniel Peñalba


People also ask

Can you draw in C#?

Drawing in C# is achieved using the Graphics Object. The Graphics Object takes much of the pain out of graphics drawing by abstracting away all the problems of dealing with different display devices and screens resolutions. The C# programmer merely needs to create a Graphic Object and tell it what and where to draw.

What is the syntax of draw text method?

The DrawText function draws formatted text in the specified rectangle. It formats the text according to the specified method (expanding tabs, justifying characters, breaking lines, and so forth). To specify additional formatting options, use the DrawTextEx function.

What is using system drawing in C#?

The Graphics class provides methods for drawing to the display device. Classes such as Rectangle and Point encapsulate GDI+ primitives. The Pen class is used to draw lines and curves, while classes derived from the abstract class Brush are used to fill the interiors of shapes.

What is drawString method in Java?

The drawString() method, shown below, takes as parameters an instance of the String class containing the text to be drawn, and two integer values specifying the coordinates where the text should start. public void paint(Graphics g) { g. drawString("abc", 25, 25); }


2 Answers

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);
    }
}
like image 108
Hans Passant Avatar answered Oct 05 '22 01:10

Hans Passant


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.

like image 23
Abbas Avatar answered Oct 05 '22 00:10

Abbas