Click here to Skip to main content
14,643,165 members
Articles » Multimedia » GDI » General
Posted 26 Jul 2015


20 bookmarked

GDI DrawString Configurator App

Rate this:
4.88 (13 votes)
Please Sign up or sign in to vote.
4.88 (13 votes)
26 Jul 2015CPOL
This app helps you with understanding and using GDI DrawString function: precise measuring, positioning, quality setting and performance tuning.


I presume you have already encountered the GDI DrawString function - in .NET, it is represented by System.Drawing.Graphics.DrawString method. In most cases, it is easy and straightforward, but in some advanced cases, you might find yourself struggling with combinations of various flags and settings to get the desired result. This app could be your tool to do this experimenting quickly and easily.

Image 1

Using the App

This is a brief list of functionalities you might find useful in the code:

  • Basic usage of DrawString method with basic settings (font, size, style).
  • Working with font families: listing, using generic families, determining various style support by font families, observing impact of selected font to positioning.
  • Testing text clipping and trimming by resizing the window so the whole text cannot fit to the specified area
  • Comparing the result with GraphicsPath.AddString method used for drawing text outlines.
  • Basic text output measuring using MeasureString and MeasureCharacterRanges methods and observing impact of various flags to the results.
  • Measuring and computing more font metrics such as leading or ascent (text baseline).
  • Experimenting with various flags and settings of StringFormat object.
  • Impact of text-related properties of Graphics object to text rendering quality.
  • Benchmarking the rendering performance using different settings.

Points of Interest

I have discovered some interesting things during development of this app and am using it for my purposes while developing another application.

  1. DrawString method adds some padding on the left side and right side of the text. If you want the text to start precisely on some X coordinate, you need to measure this padding and adjust your start point accordingly. MeasureString doesn't help here, you need to use MeasureCharacterRanges.

    Image 2

  2. If you want to be able to position characters really precisely, just using MeasureCharacterRanges is still not good enough, because the result numbers are rounded. You need to also activate the NoClip flag to get the precise floating-point numbers.

    Image 3

  3. The characters do not hesitate to draw also outside their measured ranges. Maybe this is the reason for using text padding.

    Image 4

  4. Yes, GDI handles kerning, as you can see in the following picture:

    Image 5

  5. If you experiment with drawing text outline, you will discover it could be sometimes misaligned with standard DrawString. Sometimes, it depends solely on the font size. If the texts are misaligned, it seems that MeasureString matches with GraphicsPath.AddString method (outlined text) while MeasureCharacterRanges goes precisely with standard DrawString method.

    Image 6

  6. Clipping does not affect GraphicsPath.AddString method.

    Image 7

I am sure you will find out lot of things on your own. I hope this app will help with solving your problems. Good luck!


  • v 1.0 - Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Petr Bříza
Software Developer (Senior) Petr Bříza
Czech Republic Czech Republic

Freelance .NET developer from Prague, Czech republic. You have a problem - I am the solution.

Visit me at

Comments and Discussions

Suggestionto bypass ranges count limitation Pin
Dev Tools4-Mar-19 4:53
MemberDev Tools4-Mar-19 4:53 
Generalres Pin
Member 1182388517-Nov-17 19:11
MemberMember 1182388517-Nov-17 19:11 
QuestionstringFormat settings and graphics settings Pin
Alex M.H.20-Jan-16 0:13
MemberAlex M.H.20-Jan-16 0:13 
QuestionVS Incompatible... Pin
AchLog28-Jul-15 2:41
MemberAchLog28-Jul-15 2:41 
Generalthanks Pin
Hooman_Kh26-Jul-15 11:09
MemberHooman_Kh26-Jul-15 11:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.