0

Adding a private Font works fine on my local dev machine with Windows 10 Pro. However on Windows Server 2012 R2 the very same code results in the following error:

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Text.PrivateFontCollection.AddFontFile(String filename)

var privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile("Roboto-Regular.ttf");

When I delete the file it results in a NotFoundException, so the file path has to be right. I ensured that every user has every possible permission on the file. (I found out that if I deny every permission on my local win 10 machine the same "generic error" occurs - so it might be a permission problem)

Has anybody a solution for this problem? Could it be possible that the service user on the productive machine lacks some other rights that are necessary for "installing" private fonts? Maybe the font gets installed to a specific directory which is not allowed - but I could not find out where this would be. Please help me with this very self explaining "generic error".

1 Answers1

1

It seems to have been a problem with not using an absolute path. Didn't think of it at first, because deleting the file resulted in an explicit FileNotFound. Using an absolute path did the trick.

  • It does go from bad to worse, you cannot call FreeCoTaskMem() until *after* you called PrivateFontCollection.Dispose(). The memory corruption bug is usually too subtle to notice, it doesn't throw AVE often enough. The original problem is probably something very silly but you can't find out until you use, say, File.ReadAllBytes(path) to get a better exception. *Always* use a full path name, c:\foo\bar\baz.ttf instead of baz.ttf. – Hans Passant Oct 04 '16 at 09:44
  • Thanks! Hmm... "File not found" might have been useful as an exception message... – komodosp Oct 27 '21 at 09:30