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.
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
.
- 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.
- The characters do not hesitate to draw also outside their measured ranges. Maybe this is the reason for using text padding.
- Yes, GDI handles kerning, as you can see in the following picture:
- 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.
- 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