This is a guide on how to add, use and work with a ttf font when you don't have it installed on your system.
With this howto you'll be able to:
- c# add font
- new font add in C#
- install fonts with c sharp
- c# font ttf
- adding ttf file in vs 2010
- how to dynamically install a .ttf file using c#
- install font c# ttf
- C# add Font to Project
- ttf c#
- c#" visual studio adding fonts
- c# add font to a solution
- add font c#
- True type fonts in C#
- c# 2010 add ttf
- how to install font visual studio
Begins here...
When you create a C# solution "Window Form", or project in Visual C# Express, Microsoft Visual Studio 2005/2008/2010, you can select the fonts easily just by selecting them from the properties.
But the problem exists when you want to use a specific TTF True type font, that could or could not be installed in the system.
Then, you need to include them into the resources of your project, so it can be obtained as a bytes array that will be loaded through a stream to a PrivateFontCollection object.
Here are the 6 steps I did to perform this.
Step 1:
Add the .ttf file to Resources (right click on your solution and select properties see the screenshot below)
Step 2:
A tab will be displayed with options and properties for our project.
Go to Resources > Add Existing File :
Look for the .ttf file in your Hard Drive:
Step 3:
Then we change the properties of the added resource (the .ttf file).
Select the FileType option and change it to "Binary".
Step 4:
Go back to your Solution Explorer view.
You will now see the Resources Directory created.
Choose your font file under this directory and select "Properties".
Change the Build Action option to: "Embedded Resource" (so the bytes array will be inside the .exe file)
(in this way there's no need to install the font in another machine).
Step 5: (Code)
Add this:
Text Version:
using System.Runtime.InteropServices;
using System.Drawing.Text;
Inside the class make a call to gdi32.dll.
[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
Note that I've created two objects, one FontFamily object called "ff" and another Font object called "font".
FontFamily ff;
Font font;
Now two methods:
One Loads the PrivateFontCollection object and the other just applies the font to the label.
The first one
**CargoPrivateFontCollections means (LoadPrivateFontCollection)**
Text Version
private void CargoPrivateFontCollection()
{
// Create the byte array and get its length
byte[] fontArray = FuentesTTF.Properties.Resources.Baby_Universe;
int dataLength = FuentesTTF.Properties.Resources.Baby_Universe.Length;
// ASSIGN MEMORY AND COPY BYTE[] ON THAT MEMORY ADDRESS
IntPtr ptrData = Marshal.AllocCoTaskMem(dataLength);
Marshal.Copy(fontArray, 0, ptrData, dataLength);
uint cFonts = 0;
AddFontMemResourceEx(ptrData, (uint)fontArray.Length, IntPtr.Zero, ref cFonts);
PrivateFontCollection pfc = new PrivateFontCollection();
//PASS THE FONT TO THE PRIVATEFONTCOLLECTION OBJECT
pfc.AddMemoryFont(ptrData, dataLength);
//FREE THE "UNSAFE" MEMORY
Marshal.FreeCoTaskMem(ptrData);
ff = pfc.Families[0];
font = new Font(ff, 15f, FontStyle.Bold);
}
THE SECOND METHOD JUST LOADS THE LABEL CALLED "label1"
(the "W" is over this.label1.Font = new Font(ff, 20, fontStyle); (SORRY)
Text Version
private void CargoEtiqueta(Font font)
{
float size = 11f;
FontStyle fontStyle = FontStyle.Regular;
this.label1.Font = new Font(ff, 20, fontStyle);
}
Step 6:
Call the two methods "CargoPrivateFontCollection()" and "CargoEtiqueta(Font font)",
I made the call from the Load event in the windows form but do it where you please, so the font is displayed correctly from the beggining. (it could be called from a button or thousands of places more)
Text Version
private void Form1_Load(object sender, EventArgs e)
{
CargoPrivateFontCollection();
CargoEtiqueta(font);
}
FINAL STEP: DONE!
IT's READY
"ESTA ES LA FIEMTE!" means "THIS IS THE FONT!"
NOTE: IMPORTANT!!!!!!
WHEN CREATING THE FONT IN THE "CargoPrivateFontCollection()" method
make sure you choose a FontStyle that's suitable for your font, otherwise it won't work and it will just display a normal default font.
Make sure you know what FontStyles your .ttf can handle
Hope it helps.
LOTS OF .TTF FREE HERE www.creamundo.com
YOU SHOULD ALSO READ C# HOW TO LOAD A FONT FROM A FILE DIRECTLY
NEED LEGAL FONTS ??
GET THIS ONES
awesome thanx a lot
ReplyDeleteThanks! really helped..
ReplyDeleteWow! Very helpfull. Thanks so much ^.^
ReplyDeletethank you for visiting the site !!
Delete:)
Clean and quick to implement. Thanks!
ReplyDeleteReally helpful. Thanks!
ReplyDeleteThis comment has been removed by the author.
Deleteyou are welcome
Deleteman should i make a private void CargoPrivateFontCollection() fo every font?
ReplyDeleteJust create a seperate class called 'Fonts' and then rename the 'CargoPrivateFontCollection()' to the name of the font. You'll need to make the class return a font family for you to use whenever you need to set the font, but from then on, you'd be able to use the font anywhere in your program by calling 'fontFamilyVariable = Fonts.Arial', and then set your text to 'text.Font = new Font(fontFamilyVariable, fontSize, fontStyle)'
DeleteAnd yes, you would need that CargoPrivateFontCollection or FontName class for each individual font.
thanks for answering
DeleteThis was greats thanks. Any idea on how to "enable" a webbrowser component inside my form to also be able to use this font?
ReplyDeleteI'm glad it helped.
Deletenot sure about what you are planning to do
please how do i add a new font to the private collection?
ReplyDeletewhat is FuentesTTF in CargoPrivateFontCollection();?
ReplyDeletei am getting error in this.
how to apply on Document Print : e.Graphics.DrawString(,,,)
ReplyDeletei try but the no result on doc preview
I'm not sure what you are trying to do.
DeleteIt's been a while since your question.
I am sure you solved it by now.
How did you do it ?
I'm making a language flash card program; and the VS installed fonts were unusable for certain characters. I'm still baffled that my PC has great fonts but that VS cannot use them.
ReplyDeleteAt any rate, I did exactly what it says in this blog, and now my program is showing the font I wanted.
Thank you very much.
Thank you for the feedback !!!
DeleteVery useful !! Thank you very much! :)
ReplyDeletewhat is FuentesTTF in CargoPrivateFontCollection();?
ReplyDeletei am getting error in this.
Replace "FuentesTTF" with the name of your namespace. Most likely, your namespace is the name of your Visual Studio project name.
Deleteyes, that's one solution to it.
DeleteHow to use same method with crystal reports .. i am facing same problem with crystal report in visual studio 2010. Please help
ReplyDeleteI installed 2 new fonts ("HAN NOM A" and "HAN NOM B"), both files with .ttf extensions.
ReplyDeleteMS Word and MS Access can used them but C# 2010 InstalledFontCollection does not have them. What is the problem? Please help me.
My email: nnamhong@gmail.com
Are you bored of using routine fonts on you smart phone? then this Stylish Fonts Free app will help you to try different Stylish Fonts on your smartphones.
ReplyDeleteHi, I know this is an old post, but can you make this code available under the MIT license so we can use it in our code? It works great, thanks!
ReplyDeleteImage is not displayed
ReplyDelete