C# How to Add Fonts ttf True Type Fonts to your Window Form application in Visual Studio. (UPDATED)




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




28 comments:

  1. awesome thanx a lot

    ReplyDelete
  2. Thanks! really helped..

    ReplyDelete
  3. Wow! Very helpfull. Thanks so much ^.^

    ReplyDelete
    Replies
    1. thank you for visiting the site !!

      :)

      Delete
  4. Clean and quick to implement. Thanks!

    ReplyDelete
  5. man should i make a private void CargoPrivateFontCollection() fo every font?

    ReplyDelete
    Replies
    1. Just 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)'

      And yes, you would need that CargoPrivateFontCollection or FontName class for each individual font.

      Delete
    2. thanks for answering

      Delete
  6. This was greats thanks. Any idea on how to "enable" a webbrowser component inside my form to also be able to use this font?

    ReplyDelete
    Replies
    1. I'm glad it helped.
      not sure about what you are planning to do

      Delete
  7. please how do i add a new font to the private collection?

    ReplyDelete
  8. what is FuentesTTF in CargoPrivateFontCollection();?
    i am getting error in this.

    ReplyDelete
  9. how to apply on Document Print : e.Graphics.DrawString(,,,)
    i try but the no result on doc preview

    ReplyDelete
    Replies
    1. I'm not sure what you are trying to do.
      It's been a while since your question.
      I am sure you solved it by now.
      How did you do it ?

      Delete
  10. 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.

    At 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.

    ReplyDelete
  11. Very useful !! Thank you very much! :)

    ReplyDelete
  12. what is FuentesTTF in CargoPrivateFontCollection();?
    i am getting error in this.

    ReplyDelete
    Replies
    1. Replace "FuentesTTF" with the name of your namespace. Most likely, your namespace is the name of your Visual Studio project name.

      Delete
    2. yes, that's one solution to it.

      Delete
  13. How to use same method with crystal reports .. i am facing same problem with crystal report in visual studio 2010. Please help

    ReplyDelete
  14. I installed 2 new fonts ("HAN NOM A" and "HAN NOM B"), both files with .ttf extensions.
    MS 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

    ReplyDelete
  15. 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.

    ReplyDelete
  16. Hi, 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!

    ReplyDelete

2 ads