Watch out for SizeF.ToSize()

I've been bitten several times lately from code that performs a MeasureString call, which returns a SizeF structure, then converts that SizeF to a Size using the ToSize() method. The problem is that ToSize() performs a standard round on the dimensions. When measuring a string, a ceiling is usually the desired behavior but there is no Ceiling() member method of SizeF. Ceiling() rather, is a static method of Size, so this:
s = g.MeasureString(Text, Font).ToSize();
becomes:
s = Size.Ceiling(g.MeasureString(Text, Font));
Just wanted to share this possible caveat.

Comments

Popular posts from this blog

Don't Repeat Yourself... Really!

Issues raised by polymorphism in relation land

Camtasia Studio Tips