0

Hi,

I am developing an ASP .NET website that needs to create images using a custom font. Obviously, to do this, I need the font installed on the web server. As I am using a hosting site, and I have no direct access to the server the site will be hosted on, I need a script that will install the font on the server.

I have found the following solution online that uses the Windows API and theoretically should install the font:

...
using System.Runtime.InteropServices;
...
public partial class InstallFont : System.Web.UI.Page
{
    [DllImport("gdi32", EntryPoint = "AddFontResource")]
    public static extern int AddFontResourceA(string lpFileName);

   protected void Page_Load(object sender, EventArgs e)
   {
        ...
        int result = -1;

        result = AddFontResourceA(@"C:\ExampleFont.TTF");
        Response.Write((result == 0) ? "Font is already installed." : 
        "Font installed successfully.");
   }
}

This runs like a charm and tells me that the font has been installed, however, when I try to create a Font object with the font that was supposedly just installed, it does not work.

I am sure that AddFontResource is working though because when I check Microsoft Word (for testing purposes) the font shows up in its list of fonts present on the machine.

Does anyone know why .NET's font object is not recognizing the font when it has been installed and is available to other programs? It may be possible that I might have to add a registry value, but I know virtually nothing about doing that.

Any help is greatly, greatly appreciated.

Edited by drspock: n/a

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by sknake
0

Do you have to register the font? I have seen code snippets before that use unregistered fonts.

// Be sure to dispose your PrivateFontCollection
// and Font to avoid an easy leak!
System.Drawing.Text.PrivateFontCollection privateFonts = new PrivateFontCollection();
privateFonts.AddFontFile("mycustomfont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
this.label1.Font = font;

Borrowed from here

0

Thank you so much! It works perfectly!

I can't believe the solution was so simple after I spent hours scouring the internet!

If only MSDN had mentioned this in their article about the Font class rather than saying that it is required that the font be installed on the computer.

0

You're welcome :)

Please mark this thread as solved if you have found a solution to your question and good luck!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.