来自相对路径的C#PrivateFontCollection AddFontFile

时间:2019-03-28 13:24:50

标签: c# model-view-controller fonts privatefontcollection

我在根目录下的此文件夹中有OpenSans-Light.ttf:〜/ fonts / open-sans / OpenSans-Light.ttf

PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile("~/fonts/open-sans/OpenSans-Light.ttf");

但是我收到“ System.IO.FileNotFoundException:找不到文件”

如何定位该字体文件?

1 个答案:

答案 0 :(得分:0)

我找到了使用MapPath的解决方案:

PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(HttpContext.Current.Server.MapPath("~/fonts/open-sans/OpenSans-Light.ttf"));