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

拟墨画扇 提交于 2019-12-10 03:36:36

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/3909520/how-to-manage-custom-fonts-in-web-application-system-drawing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!