Thursday, October 28, 2010

Graphics.DrawString vs. Path.AddString

In my Card Maker application I recently added support for drawing elements / text with an outline. Unfortunately in doing so I ran into a major difference in the scale at which Graphics.DrawString and Path.AddString draw. The scale of the font drawn by Graphics.DrawString is about 4/3 the size of one drawn by Path.Addstring

(... removed old terrible solution ...)

My original solution to the problem was a hack using a magic value. You should be looking at the Font.Unit and checking if it is GraphicsUnit.Point. If so then should be converting the font size to the EM size like so:

Graphics.DpiY * (zFont.Size / 72f)
  
If the Font.Unit is another value you should research further. Thanks to Mike Garrett for his comment. The documentation for the GraphicsUnit.Point (and other values) on MSDN reveals some of the mystery.

1 comment:

  1. For future internet travellers landing here: this difference seems to be due to the fact that the Font.Size is in Point units, and the Path size
    is in em units (and your em units may vary from machine to machine). Convert them as follows:

    float emSize = graphics.DpiY * font.Size / 72f;

    Reference: http://stackoverflow.com/questions/2292812/font-in-graphicspath-addstring-is-smaller-than-usual-font

    ReplyDelete