Font height in compact framework

In the CLR/.Net compact framework runtime … which is to say, Windows Forms on Windows Mobile … it’s not possible to ask a Font object its Height. You’d think you’d be able to say…


Font font = ...
float height = font.Height;

…but you can’t. Instead, in addition to a Font object, you need a Graphics object (which you probably have lying around), and you also need a sample string. The trick is to call the Graphics object’s MeasureString method…


Graphics g = ...
Font f = ...
float lineHeight = g.MeasureString("wibble", font).Height;

Leave a comment