Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

GDI DrawString Configurator App

0.00/5 (No votes)
26 Jul 2015 1  
This app helps you with understanding and using GDI DrawString function: precise measuring, positioning, quality setting and performance tuning.

Introduction

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.

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.

  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.

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

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

  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.

  6. Clipping does not affect GraphicsPath.AddString method.

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!

History

  • v 1.0 - Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here