1

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?

Martin
  • 2,302
  • 2
  • 30
  • 42
  • Are you doing it on localhost or stage/production env with additional paths? – Nathan Oct 12 '16 at 22:25
  • 2
    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. – Hans Passant Oct 12 '16 at 22:30
  • @HansPassant Thanks! Please submit this comment as an answer so I can accept it. The problem was the font - not the code. – Martin Oct 13 '16 at 11:20
  • Surely you can complete your Q+A yourself now? You don't need my help. – Hans Passant Oct 13 '16 at 11:41

1 Answers1

1

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.

Martin
  • 2,302
  • 2
  • 30
  • 42