.Net Font Caching?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

We have a .Net 1.1 application that runs as a Service. It prints to various
printers. It uses a custom font that we have created.

Sometimes when we install the application on a new server and install the
font, we have to reboot the server *twice* before the application recognizes
the font.

The first time we reboot, the application does not recognize the font. It
prints with a generic font that looks like Arial. However, if we start up
WordPad, WordPad *does* recognize the font, and prints properly.

We have tried installing the font by copying the TTF file to the Fonts
folder, and by using the Control Panel Font application, with similar results.

We create text for printing by means of the Graphics.DrawString method.

Is there any explanation for this, and/or way around it?
 
Hi Howard,

Thanks for your posting. As for the adding new font problem you mentioned,
I've done some tests on my side and it seems that the .net can detect the
new added system fonts (in the windows(winnt)/fonts folder) without restart
the computer. In fact, the .net do cache the system font collection but
that's only during the lifecyle of a .net application(appdomain). When you
restart the application, and access the system fonts again, the changes
should be reflected in the new AppDomain. To verify this, you can create a
simple winform application and add the following code:

System.Drawing.Text.InstalledFontCollection ifc = new
InstalledFontCollection();
System.Text.StringBuilder sb = new System.Text.StringBuilder();

foreach (FontFamily fm in ifc.Families)
{
sb.Append("\t" + fm.Name );

}

MessageBox.Show(sb.ToString());


the InstalledFontCollection.Families contains all the currently installed
True Type fonts under the system fonts folder. You can run the code after
you install your custom font to see whether it display in the collection.
If that shows the new added font, the .net has correctly detect the new
font, we may need to do some further shoot in the code we use the font.

In addition, we can also use the System.Drawing.Text.PrivateFontCollection
to use a custom True Type Font(.ttf) without installing it into the system
fonts( as a temporary font). For example:

#the following code use a Custom font to draw some string into a image and
output to a web page. The custom font isn't added to the system , we
directly use it via locating its "ttf" file.

================================
private void Page_Load(object sender, System.EventArgs e)
{
try
{
System.Drawing.Text.PrivateFontCollection pfc = new
PrivateFontCollection();

//locate the font family's ttf file

pfc.AddFontFile(Server.MapPath("DarkGardenMK.ttf"));

FontFamily family=new FontFamily("Dark Garden",pfc);
Font dgFont=new Font(family,40);

Bitmap tmpBitmap = new Bitmap(400,100,PixelFormat.Format32bppArgb);
Graphics objGraphics = Graphics.FromImage(tmpBitmap);


objGraphics.DrawString("Hello World!", dgFont, new SolidBrush(Color.White
),0,0);

Response.Clear();
Response.ContentType = "image/jpeg";
tmpBitmap.Save(Response.OutputStream,ImageFormat.Jpeg);

Response.End();
tmpBitmap.Dispose();

}
catch(Exception ex)
{
Response.Write("<br>" + ex.Message);
}

}

==================================

This will be very useful when we don't want to install a certain custom
font into the system and still able to use it in our own application. Hope
also helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Steven,

Thanks for the information. We will investigate this further using the
classes you described and reply if we learn anything.

Maybe it has something to do that our process is running as a service?

--Howard
 
Thanks for the response Howard,

Is is possible that the .net FontCollection may behave different in a
normal desktop app from a NT service. I suggest you try making a simple NT
service and try the Classes I mentioned to do some tests to verfiy how the
net will react when you adding a new true type font on the server machine.

Anywway, I'll wait for your good news.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
OK, for the time being we are going to try changing the procedure so the font
is installed before the service is installed.

If that doesn't fix the problem we may try troubleshooting further.
 
Thanks Howard,

Please feel free to let me know if you have any progress or need assistance.
Good Luck!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Instead of installing fonts, a much better and clean option is to embed font
in the assembly and dynamically load it. You never have to bother with font
installation anymore. Plus you dont have to deal with all those problems
that you have mentioned.

Let me know if you need code.

Ali


Hello (e-mail address removed),
 
Sure, some code hints would be useful.

ali said:
Instead of installing fonts, a much better and clean option is to embed font
in the assembly and dynamically load it. You never have to bother with font
installation anymore. Plus you dont have to deal with all those problems
that you have mentioned.

Let me know if you need code.

Ali


Hello (e-mail address removed),
 
Back
Top