Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / Windows Forms

Load And Use Custom Font Without Installing It

Rate me:
Please Sign up or sign in to vote.
4.87/5 (17 votes)
7 Sep 2010CPOL1 min read 101.6K   33   9
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:

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

Result

This article was originally posted at http://c-sharpening.blogspot.com/feeds/posts/default

License

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


Written By
Software Developer (Senior) i-BLADES
Thailand Thailand
I'm Android and Full Stack Software Engineer. 28 years in software industry, lots of finished projects. I’m never afraid of learning something new and you can see it by amount of skills in my resume.

I'm working remotely since 2009, self-motivated and self-organized.

There are no impossible projects. I have experience with Android, iOS, Web, Desktop, Embedded applications, VR, AR, XR, Computer vision, Neural networks, Games, IoT, you name it.

Comments and Discussions

 
QuestionUsing the specific font (the un-installed one) in a Word document Pin
Member 131594447-May-17 3:02
Member 131594447-May-17 3:02 
AnswerRe: Using the specific font (the un-installed one) in a Word document Pin
Member 1315944420-May-17 9:54
Member 1315944420-May-17 9:54 
QuestionThis works if you have simple fonts. Delete all your non-simple fonts. Pin
Member 899937628-Mar-16 5:22
Member 899937628-Mar-16 5:22 
This works if you have simple fonts. Delete all your non-simple fonts. Some fonts are just problematic and there are so many free commercial fonts out there, just delete the problem ones. The fonts can be in a sub-directory. Just put .font in front of the filename in the listing of embedded objects in the .vsproj file. The "." is like a backslash to a directory.
QuestionThe name 'name' does not exist in the current context error Pin
Member 1142913828-Oct-15 1:59
Member 1142913828-Oct-15 1:59 
QuestionThank you for your Blog Pin
Shivachalappa Gotur6-Aug-15 2:03
Shivachalappa Gotur6-Aug-15 2:03 
Questionif getting error " The type or namespace name 'PrivateFontCollection' could not be found " add the line "using System.Drawing.Text" Pin
Shivachalappa Gotur6-Aug-15 2:02
Shivachalappa Gotur6-Aug-15 2:02 
NewsThanks Pin
Pouriya Ghamary15-Dec-10 5:45
Pouriya Ghamary15-Dec-10 5:45 
Questionhow to use custom font using C++ - VS 2010 Pin
ktn080529-Sep-10 14:21
ktn080529-Sep-10 14:21 
GeneralMy vote of 5 Pin
Bogdan Marian13-Sep-10 19:34
professionalBogdan Marian13-Sep-10 19:34 

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

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