Using custom fonts on a Label on Winforms

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?


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!


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


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