Click here to Skip to main content
Click here to Skip to main content

Load And Use Custom Font Without Installing It

, 7 Sep 2010
Rate this:
Please Sign up or sign in to vote.
How to load and use font, not installed in the system?

Introduction

How can you load and use fonts that are not installed in the system? Your application does not always have enough rights to install custom font into system. For example in ClickOnce application.

Background

I wrote an application for ClickOnce install. My application requires a custom font. How could I use custom font without administrator privileges? I looked at the MSDN documentation, found class PrivateFontCollection and saw a beautiful example. Three seconds and I had a few lines of code in my app. But nothing happened. Custom font didn't appear!

Ok, I wrote a test program, and used a complete example from MSDN. Same result! Looking at the example, I saw used font names - Arial, Courier New, Times New Roman... Why am I not surprised that this example works? Cause these fonts are preinstalled in the system. Only a complete idiot will delete these fonts!

I searched the internet and saw something about SetCompatibleTextRenderingDefault method of Application. Visual Studio by default sets this method to false but for rumors should be true. I tried, but still got nothing.

More Googling and I saw similar examples, the only difference was that the new Font was created using FontFamily, but not by face name, as in the MSDN sample.

Solution

I tried to use FontFamily received from PrivateFontCollection and gotcha! The result is fine! If I don't forget, I will send feedback to MSDN. (Update: Already sent)

Sample

Create an empty Windows Forms project, add label on form. Add Form.OnLoad handler, add the following lines:

PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile("C:\\Path To\\PALETX3.ttf");
label1.Font = new Font(pfc.Families[0], 16, FontStyle.Regular);

Result

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ernest Poletaev
Software Developer (Senior) Enterprise Information Systems, Plc
Thailand Thailand
Senior Software Developer in Enterprise Information Systems, an ERP / CRM / Docflow Software Solution Provider for Russian Local Market.
 
Professional Developer (C, C++, C#), since 1984. Experienced in many programming technologies.
 
Now i resides in Kalasin, Thailand, working remotely for my company

Comments and Discussions

 
NewsThanks PinmemberPouriya.GH15-Dec-10 5:45 
Questionhow to use custom font using C++ - VS 2010 Pinmemberktn080529-Sep-10 14:21 
GeneralMy vote of 5 Pinmembersatrapu'13-Sep-10 19:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 7 Sep 2010
Article Copyright 2010 by Ernest Poletaev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid