Rendering Text Using the .NET Framework

The .NET framework uses GDI+ for the System.Drawing namespace, and this namespace has a whole new way of drawing text. Rather than using the venerable TextOut and ExtTextOut functions, there’s Graphics.DrawString() and Graphics.MeasureString().

The problem with these is that text kerning is measured differently – more accurately – but in a way that is often incompatible with the way app developers want to use these functions.

Specifically, adding a character to a string can change the position of the characters before it. This means if you’re writing an editor and using Graphics.DrawString() to render text with a proportional font, the string will kind of jump around as the user types.

ExtTextOut wasn’t smart enough to do this sort of kerning, so you don’t have this problem. Unfortunately the .NET framework doesn’t have any text drawing function that you can fall back on, so the only way to get the old behaviour is through P/Invoke to the ExtTextOut function in GDI.

This means turning your simple DrawString call:

1
g.DrawString(line.Text, f, Brushes.Black, new Point(0, line.Y));

into

1
2
3
4
5
6
// Initialize destRect to something suitable
RectangleF destRect = new RECT();

IntPtr dc = g.GetHdc();
ExtTextOut(dc, 0, line.Y, 0, ref destRect, line.Text, (uint)line.Text.Length, null);
g.ReleaseHdc();

You also need to declare the signature of ExtTextOut for P/Invoke; the code for that is here (and the code for RECT is here).

Update: The .NET Framework 2.0 is now released. With 2.0 Microsoft acknowledged the need for the original functionality, and added a new class, TextRenderer. This class adds methods for DrawString and MeasureString, so the code snippet above can be replaced with:

1
TextRenderer.DrawText(g, line.Text, font, new Point(0, line.Y), Color.Black);

Much nicer.

2 Responses to “Rendering Text Using the .NET Framework”

  1. Arthur M Says:

    One of the big problems with DrawString function is its total ignorance to character based operations, that combined with string implementation in .NET (strcpy every time you do something with a string)…. I just wonder why is it wizards at Microsoft thought that nobody will need to work with characters now that we have strings :)

    the cost of invalidation and repainting in .net is huge comparing to other operations and if you choose to optimize your application for speed, there seems to be virtually no other way then to forget abotu GDI+ and use GDI instead. What am i missing here?

    arthurmnev at yahoo dot com

  2. Chris A Says:

    Oh dear god in heaven thank you! I’ve spent two days trying to figure out why on earth the text I was drawing in an TreeView.OnDrawNode override was too long to fit in the original bounds, with all the same parameters. Extremely frustrating.

Leave a Reply

buy cheap Endep online buy cheapest Endep and Endep how to order Amitriptyline online without prescription buy Amitriptyline without rx from us pharmacy online Amitriptyline purchase purchase online prescription Amitriptyline without purchasing Amitriptyline online without prescription order generic Amitriptyline buy Endep no prescriptions how to order Endep online without prescription comprare Endep generico Endep online buy Endep in england buy cheap Endep online free consult Endep purchased online without prescription what is Endep Endep tabletten buy Endep no perscription cod Endep non rx fedex overnight free want to buy Anafranil in usa Aricept overdose buy discount Aricept on line Aricept buy Aricept online Aricept order buy Aricept pills Nizoral suppliers cheap order rx Nizoral canada Nizoral order Nizoral usa cod comprar Nizoral generico buy Nizoral 200 mg buy cheap Nizoral under without rx buy cheap Nizoral with dr. prescription Prozac 200 mg buy Lisinopril australia el Nizoral generico buy Lisinopril tablets without rx next day delivery Lisinopril with no script buy Lisinopril c o d purchase arimidex no scams order Arimidex order amex Arimidex canadian pharmacy buy Arimidex without doctor order rx free Arimidex buy Abilify with american express buy Abilify cod order Finpecia without a prescription cheap order rx Finpecia purchase Famvir no visa online without prescription buy Famvir paypal without rx buy Famvir without Famvir without prescription medications Famvir without persription purchase Famvir online no membership overnight shipping Famvir no physician approval Amitriptyline purchase order Amitriptyline free next day airAmitriptyline on line Valtrex no rx needed cod accepted buy next day Fluoxetine buy Fluoxetine line purchase Fluoxetine cod delivery buy Fluoxetine 20 mg purchase Fluoxetine buy cheap Fluoxetine under without rx purchase Fluoxetine visa without prescription Fluoxetine by mail Fluoxetine for pets Fluoxetine 10mg Amitriptyline purchase online how to order Famvir online without a rx Prozac Amitriptyline no perscription Famvir next day purchase Anafranil no prescription cheap buy cheapest Anafranil next day delivery on Anafranil saturday Anafranil buy Anafranil buy Anafranil overnight where to purchase Anafranil no prescription no fees Anafranil drug buy Lisinopril where Lisinopril buy on line order Lisinopril online no membership overnight shipping no prescription required for Lisinopril order maxalt cash on delivery Maxalt from canada buy cheapest Maxalt purchase Maxalt visa without prescription buy Maxalt uk Maxalt 10 mg purchase Maxalt no visa online without prescription purchase generic Lisinopril online Lisinopril no prescription to buy maxalt order online no membership overnight how to get a arimidex rx Aricept without a perscription fedex Aricept overnight without a prescription Donepezil prescription order buy genuine Aricept Aricept uk sales prescription Proscar buy Proscar pay cod Maxalt apotheke buy no perscription Proscar purchase Proscar online buy cheap fedex Prednisone maxalt with no presciption buy Maxalt shipped cod Proscar online Prednisone order buy Amitriptyline with amex Accutane 40 mg delivered overnight buy brand Accutane 40 mg Accutane 40 mg orderd online without prescription where to purchase generic Accutane 40 mg online without a rx how to get 40 mg Accutane without next day delivery 40 mg Accutane with no script buy Amitriptyline online Accutane 40 mg online no prescription buy Accutane 40 mg online us pharmacy Accutane 40 mg best buy buy Accutane 40 mg c o d order 40 mg Accutane cash on delivery no presciption 40 mg Accutane