tags:

views:

236

answers:

1

I have a .ttf file ,i want to retrieve the font family name.

+1  A: 

Here is what I have used in past, this was for web application so probably not exactly what you want. Also the Font ttf file was being stored in a database. You will need to replace the [FONTASBYTEARRAY] with an actual byte[].

There is probably a much better way to get the ttf file into the font object, but this should get you started.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Runtime.InteropServices;

namespace Utility
{
    public class Font
    {
        public string GetFont(byte[] [FONTASBYTEARRAY])
        {
            PrivateFontCollection fc = new PrivateFontCollection();
            IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement([FONTASBYTEARRAY], 0);
            fc.AddMemoryFont(pointer, Convert.ToInt32([FONTASBYTEARRAY].Length));
            System.Drawing.Font f = new System.Drawing.Font(fc.Families[0], 10);
            FontFamily ff = f.FontFamily;
            return ff.Name;
        }
    }
}
Dustin Laine