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.
Text Version
[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