gpt4 book ai didi

.NET PrivateFontCollection - 加载不同样式的字体

转载 作者:行者123 更新时间:2023-12-05 01:28:34 24 4
gpt4 key购买 nike

我正在尝试将应用程序从传统 ASP.NET 移植到 Azure,但遇到了字体问题。有一个组件可以渲染图像,并在其上以特定的非标准字体绘制文本(我们称该字体为“Water”)。

Water 以多个不同的 TTF 文件存在,每个文件代表不同的风格。我总共有12个不同的TTF文件,涵盖“Water Black Italic”、“Water Light”、“Water Regular”、“Water Ultralight”等样式。

在 ASP.NET 上,这是一个简单的情况,将字体安装在 Windows 目录中,然后通过代码调用:

Font _fontMedium = new Font("Water Medium", iFontMedium, FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

但是,在 Azure 上,您无法安装自己的字体。因此,为了能够在图像上绘制这些字体,我需要将字体文件作为应用程序的内容上传,并使用 PrivateFontCollection 类单独加载它们。

我当前的代码是这样的:

        PrivateFontCollection fontcollection = new PrivateFontCollection();
string fontpath = string.Concat(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "assets\\");
fontcollection.AddFontFile(string.Concat(fontpath,"water-bold-webfont.ttf"));
fontcollection.AddFontFile(string.Concat(fontpath, "water-ultralight-webfont.ttf"));
fontcollection.AddFontFile(string.Concat(fontpath, "water-medium-webfont.ttf"));
FontFamily[] privatefontfamilies = fontcollection.Families;


Font _fontMedium = new Font("Water Medium", iFontMedium, FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
Font _fontSmall = new Font("Water UltraLight", iFontSmall, System.Drawing.GraphicsUnit.Pixel);
Font _fontMini = new Font("Water Bold", iFontMini, System.Drawing.GraphicsUnit.Pixel);

我遇到的问题是,在添加字体文件后中断后,fontcollection.Families 集合中只有一个 Family:“Water”。

同样,在声明字体对象后,该系列默认为 Microsoft Sans-Serif。如果我改为使用:

Font _fontMedium = new Font("Water", iFontMedium, FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

字体加载了“Water”系列,但似乎无法让我正确选择样式。我唯一的字体样式选项是 FontStyle 枚举中定义的选项,它不涵盖我尝试加载的所有字体样式。

简而言之,给定一种特定字体及其所有可用样式加载到单独的 TTF 文件中,您如何将它们加载到 PrivateFontCollection 中并单独使用它们?

最佳答案

引用上面的代码,您可以尝试...

Font _fontMedium = 
new Font(privatefontfamilies[0] , iFontMedium, FontStyle.Regular,
System.Drawing.GraphicsUnit.Pixel);

而不是使用字体名称字符串参数化字体。

微软表示(来源:MSDN):

The first argument passed to the Font constructor is the font family name (not a FontFamily object as is the case for other variations of the Font constructor)

我挣扎着发现这绝对是错误的(并且不起作用)!使用字体系列的名称,您想要创建的字体将被替换为类似的字体(就像您上面所写的那样)。使用 fontfamily-object 作为参数效果很好(对我来说)。

关于.NET PrivateFontCollection - 加载不同样式的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4876745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com