CaptainBli
CaptainBli

Reputation: 4201

Font cannot be found in asp.net c# when writing to bitmap

The problem is that I cannot get a font to load and then render in a bitmap. Specifically, the asp.net web page code creates a bitmap and then tries to write out text into the bitmap. I tried moving code from one server (Win 2003) to another (Win 2008). It works on Win 2003 but not on Win 2008. I copied the fonts (.ttf) from the old server and installed them on the new server.

Code:

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();

Then it throws this exception on the line with new FontFamily():

Exception information: 
Exception type: ArgumentException 
Exception message: Font 'Free 3 of 9' cannot be found. 

The font has been installed into the Windows Font folder. If I try:

Font threeOfNine = new Font("Free 3 of 9", 60, FontStyle.Regular, GraphicsUnit.Point);

Then the Font is created with the standard default font. Are there security issues? Why won't my font load correctly?

Upvotes: 1

Views: 3929

Answers (1)

CaptainBli
CaptainBli

Reputation: 4201

Well I found the answer myself.

After installing the font and ensuring the right permissions were on the .ttf. I had to restart IIS. I think only the app pool was all that was needed to restart. Then it started working.

Upvotes: 4

Related Questions