简体   繁体   中英

How to install fonts on web server to use in graphics.DrawString method on web api

I am developing an application in .net that uses graphics.DrawString to draw text on images. this method accepts fonts as parameter, on my local I install fonts and I can use it there, but how can I install fonts on my host, (on hosting you don't have access to operating system to install fonts, you just have a panel) in my case I host it on Azure as a web app .

Then my question is how should I install fonts on web server, or maybe virtually to just use it in that function.

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            // some codes
            graphics.DrawString(text, font, brush, location, drawFormat);
        }

I found solution in case other people having same issue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM