问题是我无法加载字体然后在位图中渲染。具体来说,asp.net网页代码创建一个位图,然后尝试将文本写出到位图中。我尝试将代码从一台服务器(Win 2003)移动到另一台服务器(Win 2008)。它适用于Win 2003,但不适用于Win2008。我从旧服务器复制了字体(.ttf),并将其安装在新服务器上。
码:
Bitmap barcode = new Bitmap(1, 1);
ffamily = new FontFamily("Free 3 of 9"); //exception
Font threeOfNine = new Font(ffamily, 60, FontStyle.Regular, GraphicsUnit.Point);
Graphics graphics = Graphics.FromImage(barcode);
SizeF size = graphics.MeasureString(number, threeOfNine);
barcode = new Bitmap(barcode, size.ToSize());
graphics = Graphics.FromImage(barcode);
graphics.Clear(Color.White);
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
graphics.DrawString(number, threeOfNine, new SolidBrush(Color.Black), 0, 0);
graphics.Flush();
threeOfNine.Dispose();
graphics.Dispose();
然后,它在
new FontFamily()
的行上抛出此异常:Exception information:
Exception type: ArgumentException
Exception message: Font 'Free 3 of 9' cannot be found.
字体已安装到Windows字体文件夹中。如果我尝试:
Font threeOfNine = new Font("Free 3 of 9", 60, FontStyle.Regular, GraphicsUnit.Point);
然后,使用标准默认字体创建字体。有安全性问题吗?为什么我的字体无法正确加载?
最佳答案
好吧,我自己找到了答案。
安装字体并确保对.ttf拥有正确的权限之后。我必须重新启动IIS。我认为只有应用程序池才能重新启动。然后它开始工作。