我有一个.NET 3.5应用程序,使用PrivateFontCollection.AddMemoryFont将字体加载到内存中,并使用它们生成图像.我最近在Windows Server 2012 R2上安装了它,它产生间歇性错误.
这个方法说明了这个问题:
private Bitmap getImage(byte[] fontFile)
{
using (PrivateFontCollection fontCollection = new PrivateFontCollection())
{
IntPtr fontBuffer = Marshal.AllocCoTaskMem(fontFile.Length);
Marshal.Copy(fontFile, 0, fontBuffer, fontFile.Length);
fontCollection.AddMemoryFont(fontBuffer, fontFile.Length);
Bitmap image = new Bitmap(200, 50);
using (Font font = new Font(fontCollection.Families[0], 11f, FontStyle.Regular))
{
using (Graphics graphics = Graphics.FromImage(image))
{
graphics.DrawString(String.Format("{0:HH:mm:ss}", DateTime.Now), font, Brushes.White, new PointF(0f, 0f));
}
}
return image;
}
}
Run Code Online (Sandbox Code Playgroud)
在Windows 7上,这一切都是一致的.在Windows Server 2012 R2上,如果使用多种字体重复调用,则会失败.例如:
getImage(File.ReadAllBytes("c:\\Windows\\Fonts\\Arial.ttf"));
Run Code Online (Sandbox Code Playgroud)
即使被调用数百次,但使用多种字体调用,它仍可正常工作:
getImage(File.ReadAllBytes("c:\\Windows\\Fonts\\Wingding.ttf"));
getImage(File.ReadAllBytes("c:\\Windows\\Fonts\\Arial.ttf"));
Run Code Online (Sandbox Code Playgroud)
将适用于前几次调用(20左右),但随后将开始产生随机结果(第二次调用有时会返回带有文本的图像 - 即它正在混合字体).
我偶尔(很少)在DrawString调用中得到"GDI +中发生了一般错误".
Windows 7上不会发生这些错误.
我已经尝试了各种选项来清理而没有任何成功.
作为一种解决方法,我尝试将字体文件写入磁盘,然后使用AddFontFile加载,但是(在Windows …
.net fonts gdi+ privatefontcollection windows-server-2012-r2