Using custom fonts in C# application

By | 19. March 2016

Recently I wanted to embed custom fonts in my application. After some googling and mixing the solution is this

[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);

public static PrivateFontCollection private_fonts = new PrivateFontCollection();
public static void LoadFont()
{
// Use this if you can not find your resource System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
string resource = "Test.Images.MyFont.ttf";
// receive resource stream
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);

//create an unsafe memory block for the data
System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
//create a buffer to read in to
Byte[] fontData = new Byte[fontStream.Length];
//fetch the font program from the resource
fontStream.Read(fontData, 0, (int)fontStream.Length);
//copy the bytes to the unsafe memory block
Marshal.Copy(fontData, 0, data, (int)fontStream.Length);

// We HAVE to do this to register the font to the system (Weird .NET bug !)
uint cFonts = 0;
AddFontMemResourceEx(data, (uint)fontData.Length, IntPtr.Zero, ref cFonts);

//pass the font to the font collection
private_fonts.AddMemoryFont(data, (int)fontStream.Length);
//close the resource stream
fontStream.Close();
//free the unsafe memory
Marshal.FreeCoTaskMem(data);

}

...

// Apply  font to TextEdit

LoadFont();
tbPassword.PasswordChar = 'A';
tbPassword.Font = new Font(private_fonts.Families[0], 8);

...

Add your font file to project resources :

Embed Font

Embed Font

 

Use the code above and you are ready to go. This solution also works with DevExpress edit components

Nice tool to quickly make your font form images https://glyphter.com/

 

Other references

http://www.codeproject.com/Articles/107376/Embedding-Font-To-Resources

http://stackoverflow.com/questions/1955629/c-sharp-using-an-embedded-font-on-a-textbox

 

 

4 thoughts on “Using custom fonts in C# application

  1. kulstad

    This is extremely useful (and timely for me), thank you for this. However, how would you modify this to dynamically load more than one font? I am building a report application, and the headers of the reports need to be a different font from the detail/body section of the report (ie: report headers = “Times New Roman”, detail/body section = “Arial”).

    Thanks in advance for any insight you may have.

  2. admin Post author

    Than you for your comment!

    Why do you need to load custom fonts for report application ?
    Specially in case of your example the fonts like “Times New Roman” and “Arial” are always present in system.

    I don’t know how does your report application work, but can’t you just use standard
    … = new Font(“Arial”, 20)
    instead of complex loading external fonts?

    Also – reporting is very complex task. I use one commercial tool for most applications. But there are also available several freeware tools and maybe they are worth to look at before putting effort into writing own reporting engine. Here is one list, sure not exhaustive one, but good starting point : http://www.butleranalytics.com/5-free-net-reporting-tools/

    1. kulstad

      I used TimeNewRoman and Arial as examples, but the fonts I need to use are custom fonts that we don’t want to distribute to customers. Swap Arial and Times New Roman for CustomFont1 and CustomFont2, and my question remains the same.

      I am using Crystal Reports for our application, and have no issues with it (other than accessing the objects properly, but that’s just a minor concern).

Leave a Reply