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




48 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. Really helpful. Thanks!

    ReplyDelete
  6. 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
  7. 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
  8. please how do i add a new font to the private collection?

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

    ReplyDelete
  10. 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
  11. 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
  12. Very useful !! Thank you very much! :)

    ReplyDelete
  13. 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
  14. How to use same method with crystal reports .. i am facing same problem with crystal report in visual studio 2010. Please help

    ReplyDelete
  15. 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
  16. 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
  17. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  18. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  19. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  20. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  21. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  22. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  23. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  24. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  25. Hi there! The smaller font under each word tells the actual name of the font. The "sparkling" one is called Sweet Pea font and the link is: http://www.dafont.com/sweet-pea.font
    The clickable links to all of them are right under the image!
    Thanks!

    ReplyDelete
  26. Hi there! The smaller font under each word tells the actual name of the font. The "sparkling" one is called Sweet Pea font and the link is: http://www.dafont.com/sweet-pea.font
    The clickable links to all of them are right under the image!
    Thanks!

    ReplyDelete
  27. Hi there! The smaller font under each word tells the actual name of the font. The "sparkling" one is called Sweet Pea font and the link is: http://www.dafont.com/sweet-pea.font
    The clickable links to all of them are right under the image!
    Thanks!

    ReplyDelete
  28. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  29. Thank you! Canva revolutionised my work. I work as a PR & marketing manager in hospitality and I don’t have a big marketing budget, so Canva is perfect for me. 1$ for picture is not so much but I can create some nice offers for our clients. Here you have an example of my work: http://www.dwor.pl/pl/klienci_biznesowi/spotkania_wigilijne/ and it was only for 2 $ It’s a nice alternative for Shutterstock etc. Fonts are beautiful, it is very creative thinking to learn how to mix them. I use it also on my personal blog

    ReplyDelete
  30. Hi, i absolutely got tons of value from your post. Please i have 2 quick questions.
    1. What is the number of plugins every blogger shouldn’t exceed? I currently have about 18 installed, would you consider that number outrageous. Please could you also check out my site and offer me a
    2. Your font is really beautiful. would you suggest plugins that would give me beautiful fonts just like yours?
    Finally, would you spare a few seconds to check this site and offer me your candid advice http://www.samozoani.com

    ReplyDelete
  31. Hi, i absolutely got tons of value from your post. Please i have 2 quick questions.
    1. What is the number of plugins every blogger shouldn’t exceed? I currently have about 18 installed, would you consider that number outrageous. Please could you also check out my site and offer me a
    2. Your font is really beautiful. would you suggest plugins that would give me beautiful fonts just like yours?
    Finally, would you spare a few seconds to check this site and offer me your candid advice http://www.samozoani.com

    ReplyDelete
  32. Hi, i absolutely got tons of value from your post. Please i have 2 quick questions.
    1. What is the number of plugins every blogger shouldn’t exceed? I currently have about 18 installed, would you consider that number outrageous. Please could you also check out my site and offer me a
    2. Your font is really beautiful. would you suggest plugins that would give me beautiful fonts just like yours?
    Finally, would you spare a few seconds to check this site and offer me your candid advice http://www.samozoani.com

    ReplyDelete
  33. Hi there! The smaller font under each word tells the actual name of the font. The "sparkling" one is called Sweet Pea font and the link is: http://www.dafont.com/sweet-pea.font
    The clickable links to all of them are right under the image!
    Thanks!

    ReplyDelete
  34. Hi, i absolutely got tons of value from your post. Please i have 2 quick questions.

    1. What is the number of plugins every blogger shouldn’t exceed? I currently have about 18 installed, would you consider that number outrageous. Please could you also check out my site and offer me a

    2. Your font is really beautiful. would you suggest plugins that would give me beautiful fonts just like yours?
    Finally, would you spare a few seconds to check this site and offer me your candid advice http://www.samozoani.com

    ReplyDelete
  35. Hi there! The smaller font under each word tells the actual name of the font. The "sparkling" one is called Sweet Pea font and the link is: http://www.dafont.com/sweet-pea.font
    The clickable links to all of them are right under the image!
    Thanks!

    ReplyDelete
  36. Hi there! The smaller font under each word tells the actual name of the font. The "sparkling" one is called Sweet Pea font and the link is: http://www.dafont.com/sweet-pea.font
    The clickable links to all of them are right under the image!
    Thanks!

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