views:

2451

answers:

1

I wish to load and use a font to a desktop application in C#. It's that possible without installing the font on the system?

It's a kind of question like this but not from a DLL. I want to load from font file.

+6  A: 

There's a class System.Drawing.Text.PrivateFontCollection in System.Drawing.dll which can manage fonts on a per application basis.

All you do is that you maintain this collection within your app and you add fonts through AddFontFile or AddMemoryFont and you'll then be able to use that font as if it was installed on your system.

It's like installing the font for the application only. The font will be uninstalled once the process terminates.

John Leidegren
I think you have to keep a hold of your private font collection so that it doesn't get garbage collected. I kept getting an exception when I tried to draw using a Font object I build using data from my font collection. I figured once I had my font I didn't care about the collection, right? Apparently not. Just keep a reference to the font collection as long as you need the font. That worked for me at least.
Jon Turner
Ah, subtle, but true, I guess the GDI+ wrapper won't track those fonts for you, it gets collected while GDI+ has references to those fonts internally, and naturally it has no way of knowing that the handles are now invalid/released. The backside of Win32 introp.
John Leidegren