我已经运行了几个月的同样的代码,它的工作就像一个魅力。最近,我已经将这段代码添加到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返回值(如前所述 ),但似乎无法做到这一点。老实说,我没有更改任何其他与此相关的代码。
最奇怪的是,这是执行时的一个问题。我可以多次运行相同的构建,并得到关于可用字体的不同结果。
发布于 2016-02-26 19:31:46
结果发现,这个问题是一个ttf文件,当它被添加到源代码管理时就损坏了。关于上面的讨论,通过FreeCoTaskMem()释放内存很好(以前总是这样),因为它只需要暂时将字节送到内存中,这样就可以使用AddFontFromMem() --这是将嵌入式数据放入pfc的最简单的方法。删除错误的字体完全解决了这个问题。
https://stackoverflow.com/questions/35655611
复制相似问题