22

Is there a way to do something like this?

FontFamily fontFamily = new FontFamily("C:/Projects/MyProj/free3of9.ttf");

I've tried a variety of variations and haven't been able to get it to work.

UPDATE:

This works:

PrivateFontCollection collection = new PrivateFontCollection();
collection.AddFontFile(@"C:\Projects\MyProj\free3of9.ttf");
FontFamily fontFamily = new FontFamily("Free 3 of 9", collection);
Font font = new Font(fontFamily, height);

// Use the font with DrawString, etc.
4
  • 1
    is this path on your local ? "C:/Projects/MyProj/free3of9.ttf" if so it's / slaches need to change to ` back slaches "@C:\Projects\MyProj\free3of9.ttf" public void AddFontFile( string filename` method
    – MethodMan
    Jun 3, 2014 at 18:33
  • 3
  • could you try something like this var fontFamily = AddFontFile(@"C:\Projects\MyProj\free3of9.ttf") if you get that working then move and or create the access level variables where needed .. i.e private, Public, Protected, static etc...
    – MethodMan
    Jun 3, 2014 at 18:37
  • Yeah. Hate the PrivateFontCollection though. It wont work on Wine.
    – Bitterblue
    Sep 19, 2016 at 9:03

3 Answers 3

14

This example shows how to add font from byte array - if font is stored in resources. It allows to add font from file too. Following code I am using on winforms:

It is little tricky, for loading TTF font from file you need to do this:

private PrivateFontCollection _privateFontCollection = new PrivateFontCollection();

public FontFamily GetFontFamilyByName(string name)
{
    return _privateFontCollection.Families.FirstOrDefault(x => x.Name == name);
}

public void AddFont(string fullFileName)
{
    AddFont(File.ReadAllBytes(fullFileName));
}   

public void AddFont(byte[] fontBytes)
{
    var handle = GCHandle.Alloc(fontBytes, GCHandleType.Pinned);
    IntPtr pointer = handle.AddrOfPinnedObject();
    try
    {
        _privateFontCollection.AddMemoryFont(pointer, fontBytes.Length);
    }
    finally
    {
        handle.Free();
    }
}
1
  • Really useful approach, thought the PrivateFontCollection also has a AddFontFile method to load a font from a file path. I found unconvenient in my situation to have to know the font family name in order to get it after it have been loaded. Therefore I used the following approch in the AddFont method: 1) take a copy of the _privateFontCollection.Families (let's call it before), 2) load font, 3) create a list out of the new _privateFontCollection.Families (call it after) 4) iterate each font family in before, and remove them from after. 5) return what remains in after
    – STremblay
    Nov 12, 2019 at 16:56
11

This piece of code works for me (WPF):

FontFamily fontFamily = new FontFamily(@"C:\#FONTNAME")

In your example, it would be:

FontFamily fontFamily = new FontFamily(@"C:\Projects\MyProj\#free3of9");

The font name without the file extension, and keep the '#' symbol.

2
  • 1
    I do this exact thing in a production level project though it points to a project path instead of an absolute file path
    – Pseudonym
    Jun 3, 2014 at 19:29
  • 1
    Yes, I do the same at project level! It works in XAML files too when you define a font family in a text block in your UI, for instance.
    – bsguedes
    Jun 3, 2014 at 19:33
1

I modified the @user2126375`s trick as follows:

public PrivateFontCollection FontCollection = new PrivateFontCollection();
public FontFamily AddToFontCollection(string path)
    => AddToFontCollection(File.ReadAllBytes(path));
public FontFamily AddToFontCollection(byte[] fontBytes)
{
    var handle = System.Runtime.InteropServices.GCHandle.Alloc(fontBytes, System.Runtime.InteropServices.GCHandleType.Pinned);
    IntPtr pointer = handle.AddrOfPinnedObject();
    try
    {
        FontCollection.AddMemoryFont(pointer, fontBytes.Length);
    }
    finally
    {
        handle.Free();
    }
    return FontCollection.Families.LastOrDefault();
}

Enjoy...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.