Asp.Net GDI issues

Published January 22, 2017 10:48:29 AM  |  Last update April 12, 2019 10:54:02 PM
Tags:, mono, .net, gdi

Asp.Net GDI issues with Mono

Custom Fonts Custom fonts in Asp.Net applications is still a problem with Mono running on *nix. Being reported as a Mono bug, it however has not been addressed because of a persistent issue of GDIplus with true-type fonts on *nix systems, even if executable permission is already set to the font files.

The common way of using custom fonts in Asp.Net is shown in the following script:

# assume that the font file is copied into app folder:
using System.Drawing.Text;

PrivateFontCollection fonts = new PrivateFontCollection();
fonts.AddFontFile(Server.MapPath("~") + "/ttf-filename");
Font font = new Font(fonts.Families[0].Name, 12,
                     FontStyle.Regular, GraphicsUnit.Pixel);
… some-graphichs.DrawString(string,font…); // does not work

The only effective solution is to install custom fonts:

# Install the font to System
# assume "myfont" is a sub-folder in the system folder
mkdir -p /usr/share/fonts/truetype/myfont
cp -f  "ttf-files"  /usr/share/fonts/truetype/myfont
chmod  444  /usr/share/fonts/truetype/myfont/*ttf
# update and please make sure your fonts are detected
fc-cache -v

Once installed, the fonts are ready for your applications:

Font font = new Font("font-face", 12,
                      FontStyle.Regular, GraphicsUnit.Pixel);

Note: If "fc-cache" is not available on your system then please install "fontconfig".

web builder  tnWeb_1.54 – powered and hosted by tinyray