29

I have a label on my Winform and I want to use a custom font called XCalibur to make it appear more shnazzy.

If I use a custom font on a label and then build the solution and then .ZIP the files in \bin\Release will the end user see the labels with my custom app I used regardless if they have that font installed or not?

If this isn't the case, what's the proper way to use Custom Fonts on Labels.Text?

Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254

4 Answers4

56

After looking through possibly 30-50 posts on this, I have finally been able to come up with a solution that actually works! Please follow the steps sequentially:

1.) Include your font file (in my case, ttf file) in your application resources. To do this, double-click on the "Resources.resx" file.

enter image description here

2.) Highlight the "Add resource" option and click the down-arrow. Select "Add existing file" option. Now, search out your font file, select it, and click OK. Save the "Resources.resx" file.

enter image description here

3.) Create a function (say, InitCustomLabelFont() ), and add the following code in it.

        //Create your private font collection object.
        PrivateFontCollection pfc = new PrivateFontCollection();

        //Select your font from the resources.
        //My font here is "Digireu.ttf"
        int fontLength = Properties.Resources.Digireu.Length;

        // create a buffer to read in to
        byte[] fontdata = Properties.Resources.Digireu;

        // create an unsafe memory block for the font data
        System.IntPtr data = Marshal.AllocCoTaskMem(fontLength);

        // copy the bytes to the unsafe memory block
        Marshal.Copy(fontdata, 0, data, fontLength);

        // pass the font to the font collection
        pfc.AddMemoryFont(data, fontLength);

Your custom font has now been added to the PrivateFontCollection.

4.) Next, assign the font to your Label, and add some default text into it.

        //After that we can create font and assign font to label
        label1.Font = new Font(pfc.Families[0], label1.Font.Size);
        label1.Text = "My new font";

5.) Go to your form layout and select your label. Right-click it and select "Properties". Look for the property "UseCompatibleTextRendering" and set it to "True".

6.) If necessary you can release the font after you are sure that it can never be used again. Call the PrivateFontCollection.Dispose() method, you can then also call Marshal.FreeCoTaskMem(data) safely. It is pretty common to not bother and leave the font loaded for the life of the app.

7.) Run your application. You shall now see that you custom font has been set for the given label.

Cheers!

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Siddhant
  • 1,074
  • 12
  • 22
  • 2
    **UseCompatibleTextRendering** is not necessary if you register the font with **AddFontMemResourceEx()**. As a bonus, the font also becomes usable with TextBoxes and other controls. See [this answer](http://stackoverflow.com/a/1956043/25312) and the [MSDN documentation](https://msdn.microsoft.com/en-us/library/dd183325(v=vs.85).aspx). – SWB Mar 04 '15 at 02:24
  • Can this be added to a control's property to select one of the embedded fonts or a normal font? Ex: `private Font m_FontFace = UserControl.DefaultFont; public Font FontFace { get { return m_FontFace; } set { m_FontFace = value; } }` – Arvo Bowen Dec 08 '16 at 22:51
  • Minor recommendation for real world use, make sure the FreeCoTaskMem is in a Finally block, so in case of exceptions the memory buffer will be freed. – Rushyo Feb 22 '17 at 06:03
  • 1
    this works on the system I developed on, but doesn't work on other systems – mrid Mar 09 '18 at 14:23
37

Embed the font as a resource (or just include it in the bin directory), and then use the PrivateFontCollection to load the font (See the AddFontFile and AddMemoryFont functions). You then use the font normally like it was installed on the machine.

The PrivateFontCollection class allows applications to install a private version of an existing font without the requirement to replace the system version of the font. For example, GDI+ can create a private version of the Arial font in addition to the Arial font that the system uses. PrivateFontCollection can also be used to install fonts that do not exist in the operating system.

Source

Adam Hughes
  • 3,732
  • 24
  • 25
8

Add the font you want to use.

enter image description here

`

    PrivateFontCollection modernFont = new PrivateFontCollection();

    modernFont.AddFontFile("Font.otf");

    label.Font = new Font(modernFont.Families[0], 40);`

I made a method as well.

 void UseCustomFont(string name, int size, Label label)
    {

        PrivateFontCollection modernFont = new PrivateFontCollection();

        modernFont.AddFontFile(name);

        label.Font = new Font(modernFont.Families[0], size);


    }

enter image description here

Reza Taibur
  • 263
  • 3
  • 10
5

I think the solution is to embed the desired font into you application.

Try this link:

http://www.emoreau.com/Entries/Articles/2007/10/Embedding-a-font-into-an-application.aspx

P.K
  • 18,587
  • 11
  • 45
  • 51