Reputation: 3343
I have a .ttf file ,i want to retrieve the font family name.
Upvotes: 4
Views: 3775
Reputation: 11294
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);
Upvotes: 2
Reputation: 38503
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;
}
}
}
Upvotes: 1