Font family name from font file

 ̄綄美尐妖づ 提交于 2019-12-04 09:12:22

This is most easily done by importing the System.Windows.Media namespace, which gives you more to work with and a simpler API than getting the font out of a ByteArray

using System.Windows.Media;

String fontFilePath = "PATH TO YOUR FONT";
GlyphTypeface glyphTypeface = new GlyphTypeface(fontFileURI);
String fontFamily = glyphTypeface.Win32FamilyNames[new System.Globalization.CultureInfo("en-us")];
String fontFace = glyphTypeface.Win32FaceNames[new System.Globalization.CultureInfo("en-us")];

Console.WriteLine("Font: " + fontFamily + " " + fontFace);

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;
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!