If all you want to do is to print the name of the Font Family of each Font file stored in a directory, you can simplify your code with something like this.
It orders the Font files by name before printing the Family name.
string fontsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fonts");
var fontFiles = Directory.GetFiles(fontsPath).OrderBy(s => s).ToList();
fontFiles.ForEach(f => {
using (var fontCollection = new PrivateFontCollection())
{
fontCollection.AddFontFile(f);
Console.WriteLine(fontCollection.Families[0].Name);
};
});
If you instead want to preserve the list of the Fonts Family names (for other uses), add each Family name to a List<string>
(as a Field, here):
// As a field
List<string> fontNames = new List<string>();
// Inside a method
var fontFiles = Directory.GetFiles(fontsPath).ToList();
fontFiles.ForEach(f => {
using (var fontCollection = new PrivateFontCollection())
{
fontCollection.AddFontFile(f);
fontNames.Add(fontCollection.Families[0].Name);
};
});
fontNames = fontNames.OrderBy(s => s).ToList();
fontNames.ForEach(familyName => Console.WriteLine(familyName));
Using PrivateFontCollection.AddMemoryFont(). This method can be used for both Font Files and font data from a Font added as a Project Resource (it's just a byte array).
Important: The PrivateFontCollection
that these methods return MUST be disposed of in the Form.FormClosing
or Form.FormClosed
(or where the application's termination is handled).
Call these methods passing a collection of file paths:
// Field/Class object
PrivateFontCollection fontCollection = null;
// (...)
// Somewhere else
string fontsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fonts");
var fontFiles = Directory.GetFiles(fontsPath);
fontCollection = UnsafeLoadFontFamily(fontFiles);
// Or...
fontCollection = SafeLoadFontFamily(fontFiles);
fontCollection.Families.OrderBy(f => f.Name).ToList().ForEach(font =>
{
Console.WriteLine(font.GetName(0));
});
Using unsafe
mode and a byte* pointer:
(Unsafe code must be enabled in the Project's Properties -> Build
panel)
private unsafe PrivateFontCollection UnsafeLoadFontFamily(IEnumerable<string> fontList)
{
if (fontList.Length == 0) return null;
var fontCollection = new PrivateFontCollection();
foreach (string fontFile in fontList)
{
if (!File.Exists(fontFile)) continue;
byte[] fontData = File.ReadAllBytes(fontFile);
fixed (byte* fontPtr = fontData)
{
fontCollection.AddMemoryFont(new IntPtr(fontPtr), fontData.Length);
}
}
return fontCollection;
}
Using Marshal.AllocCoTaskMem() and Marshal.Copy().
Do not call Marshal.FreeCoTaskMem() here. Font curruption may occur. Call PrivateFontCollection.Dispose()
instead, as already mentioned.
private PrivateFontCollection SafeLoadFontFamily(IEnumerable<string> fontList)
{
if (fontList == null) return null;
var fontCollection = new PrivateFontCollection();
foreach (string fontFile in fontList)
{
if (!File.Exists(fontFile)) continue;
byte[] fontBytes = File.ReadAllBytes(fontFile);
var fontData = Marshal.AllocCoTaskMem(fontBytes.Length);
Marshal.Copy(fontBytes, 0, fontData, fontBytes.Length);
fontCollection.AddMemoryFont(fontData, fontBytes.Length);
}
return fontCollection;
}
Print the Font Collection content:
string fontPath = [The Fonts Path];
PrivateFontCollection fontCollection = null;
for (int i = 0; i < 5; i++) {
var fileList = Directory.GetFiles(fontPath, "*.ttf", SearchOption.TopDirectoryOnly);
fontCollection = SafeLoadFontFamily(fileList);
fontCollection.Families.ToList().ForEach(ff => Console.WriteLine(ff.Name));
fontCollection.Dispose();
}
System.Windows.Media
provides the Fonts.GetFontFamilies() method, just in case.