私はこの同じコードを何ヶ月も実行してきましたが、うまく機能しています。最近、このコードを TFS のソース管理に追加しましたが、正しく機能しなくなりました。唯一の変更は、製品ガイドラインを満たすためにいくつかの名前空間を変更したことです。目的はpfc
、ファイルにリソースとして埋め込まれているフォントを作成することです。その理由は、実行可能ファイルは移植可能であり、適切な .NET バージョンがインストールされていること以外には依存しないためです。
public static Dictionary<string, object> FontDict = new Dictionary<string, object>();
public static PrivateFontCollection s_FontCollection = new PrivateFontCollection();
public static Font staticFont;
public static FontFamily[] FontFamilies
{
get
{
if (s_FontCollection.Families.Length == 0)
LoadFonts();
return s_FontCollection.Families;
}
}
public static Font GetFont(string family, float size)
{
foreach (FontFamily font in FontFamilies)
{
if (font.Name.ToLower().Equals(family.ToLower()))
{
Font ret = new Font(font, size);
//return (Font)ret.Clone();
return ret;
}
}
return null;
}
public static void LoadFonts()
{
if (Assembly.GetEntryAssembly() == null || Assembly.GetEntryAssembly().GetManifestResourceNames() == null)
return;
foreach (string resource in Assembly.GetEntryAssembly().GetManifestResourceNames())
{
// Load TTF files from your Fonts resource folder.
if (resource.Contains(".Fonts.") && resource.ToLower().EndsWith(".ttf"))
{
using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream(resource))
{
try
{
// create an unsafe memory block for the font data
System.IntPtr data = Marshal.AllocCoTaskMem((int)stream.Length);
// create a buffer to read in to
byte[] fontdata = new byte[stream.Length];
// read the font data from the resource
stream.Read(fontdata, 0, (int)stream.Length);
// copy the bytes to the unsafe memory block
Marshal.Copy(fontdata, 0, data, (int)stream.Length);
// pass the font to the font collection
s_FontCollection.AddMemoryFont(data, (int)stream.Length);
// close the resource stream
stream.Close();
// free up the unsafe memory
Marshal.FreeCoTaskMem(data);
}
catch
{
}
}
}
}
FontDict.Clear();
FontDict.Add("SYS", new Font("Arial", 20));
FontDict.Add("SYSs", new Font("Arial", 14));
FontDict.Add("MICR", GetFont("MICR", 18)); // Preferred MICR font
FontDict.Add("SIG", GetFont("PWSignaturetwo", 30));
FontDict.Add("HAND1l", GetFont("Daniel", 22));
FontDict.Add("HAND2l", GetFont("Jenna Sue", 32));
FontDict.Add("HAND3l", GetFont("Honey Script", 30));
FontDict.Add("HAND4l", GetFont("Confessions", 42));
FontDict.Add("HAND5l", GetFont("Soljik-Dambaek", 26));
FontDict.Add("HAND6l", GetFont("Billy's Hand Thin", 38));
FontDict.Add("HAND7l", GetFont("Daisy Script", 34));
FontDict.Add("HAND8l", GetFont("Fineliner SCript", 34));
FontDict.Add("HAND9l", GetFont("Graphe", 20));
FontDict.Add("HAND1s", GetFont("Daniel", 16));
FontDict.Add("HAND2s", GetFont("Jenna Sue", 26));
FontDict.Add("HAND3s", GetFont("Honey Script", 24));
FontDict.Add("HAND4s", GetFont("Confessions", 30));
FontDict.Add("HAND5s", GetFont("Soljik-Dambaek", 20));
FontDict.Add("HAND6s", GetFont("Billy's Hand Thin", 26));
FontDict.Add("HAND7s", GetFont("Daisy Script", 26));
FontDict.Add("HAND8s", GetFont("Fineliner Script", 26));
FontDict.Add("HAND9s", GetFont("Graphe", 14));
FontDict.Add("HACKs", GetFont("Hack", 10)); //******************************************************//
FontDict.Add("HACKm", GetFont("Hack", 12)); // Preferred fixed-width font for non-check data //
FontDict.Add("HACKl", GetFont("Hack", 16)); //******************************************************//
突然起こり始めたのは、すべてのフォントが辞書にロードされないということです。デバッグ中にわかる限り、リソースは問題ありません。すべてのフォント リソースを正しくループします。ただし、到達すると、s_FontCollection.AddMemoryFont
一部をスキップし始めます。Status
メソッドから戻り値を読み取る方法を見つけようとしていましたが (ここで説明したように)、それを機能させることができないようです。正直なところ、これに関連する他のコードは変更していません。
本当に奇妙な部分は、実行時の問題です。同じビルドを複数回実行すると、使用可能なフォントに関して異なる結果になることがあります。