Search code examples
c#gdi+privatefontcollection

Loading font-family from disk to PrivateFontCollection


I'm using the following code to load a font into memory for generating an image with GDI+:

var fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(Server.MapPath("~/fonts/abraham-webfont.ttf"));
fontCollection.Families.Count(); // => This line tells me, that the collection has 0 items.

There are no exceptions, but the fontCollection Families property is empty after the AddFontFile method has run without any exceptions.

I've verified that the path is valid (File.Exists returns true):

Response.Write(System.IO.File.Exists(Server.MapPath("~/fonts/abraham-webfont.ttf"))); // # => Renders "True"

The TTF-file seems to work fine, when I open the file, so it's not an invalid TTF-file: https://dl.dropboxusercontent.com/u/4899329/2016-10-12_22-44-52.png

Any suggestions?


Solution

  • Answer from Hans Passant solved the problem:

    PrivateFontCollection is notoriously flakey. One failure mode that's pretty common today is that the font is actually an OpenType font with TrueType outlines. GDI+ only supports "pure" ones. The shoe fits, the web says that Abraham is an OpenType font. Works in WPF, not in Winforms.