Creating and using custom font from .ttf file in .net core 2.2 throwing CoreClr error and restarting IIS app pool

517 Views Asked by At

I am generating some images for strings using custom font files in .net core 2.2. The code works fine while I am debugging in local PC. But when I deploy it to IIS 10, it throws exception and restarts the application pool.

public FontFamily GetFontFamily(byte[] ttfFile)
    {
        FontFamily fontFamily;

        using (var memoryStream = new MemoryStream(ttfFile))
        {
            var streamData = new byte[memoryStream.Length];
            memoryStream.Read(streamData, 0, streamData.Length);
            IntPtr data = Marshal.AllocCoTaskMem(streamData.Length);
            Marshal.Copy(streamData, 0, data, streamData.Length);
            using (var privateFontCollection = new PrivateFontCollection())
            {
                privateFontCollection.AddMemoryFont(data, streamData.Length);
                fontFamily = privateFontCollection.Families[0];
                Marshal.FreeCoTaskMem(data);
            }
        }

        return fontFamily;
    }

Nothing is being logged either when I check the IIS logs. In event viewer I can see the exception as shown below.

enter image description here

1

There are 1 best solutions below

0
On

try to add the .ttf mime type by following below steps:

1)Open IIS manager.

2)Select the site from which you are getting an exception.

3)Double-click the mime types feature from the middle pane.

enter image description here

4)From the Action pane click add and add the below values:

fileExtension=".ttf"

mimeType="application/octet-stream"

Restart iis ever after making changes