January 18, 2016

AccessViolationException when using Font from PrivateFontCollection

Issue:

You have used a PrivateFontCollection to get access to a Font and are using it when suddenly you get an AccessViolationException while using the font.

Cause:

The PrivateFontCollection object has gone out of scope and has been disposed.  When the object goes out of scope, it seems to destroy any Font object that came from it.

Solution:

Move the declaration of the PrivateFontCollection so that it stays in scope until you are done with all fonts that you are using from it.