Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage custom fonts in web application (system.drawing)

I have an application that writes text onto images using System.Drawing (C#).

I am using specific fonts to do this.

Since I can't rely on my shared hosting servers to have all the custom fonts I may need (and since the list of fonts is likely to grow), how can I manage the fonts used for my application?

Could I include .ttf files in my project and reference them somehow?

What about a SQL database containing fonts?

like image 616
Brian David Berman Avatar asked Oct 11 '10 20:10

Brian David Berman


1 Answers

It should be possible to store your font files on disk or in database, and then use the PrivateFontCollection class to use the fonts at runtime.

Here is how you would use it:

    PrivateFontCollection collection = new PrivateFontCollection();
    // Add the custom font families. 
    // (Alternatively use AddMemoryFont if you have the font in memory, retrieved from a database).
    collection.AddFontFile(@"E:\Downloads\actest.ttf");
    using (var g = Graphics.FromImage(bm))
    {
        // Create a font instance based on one of the font families in the PrivateFontCollection
        Font f = new Font(collection.Families.First(), 16);
        g.DrawString("Hello fonts", f, Brushes.White, 50, 50);
    }
like image 149
driis Avatar answered Nov 05 '22 18:11

driis