字体不支持样式'Regular'-在C#中使用Semibold字体

问题描述 投票:1回答:2

我有一个TrueType字体,即“ Semibold”。我尝试在以下方法中使用它:

        private FontFamily GetFontFamily(string name)
        {
            PrivateFontCollection pfc = new PrivateFontCollection();
            var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
            pfc.AddFontFile(path);
            return pfc.Families[0];
        }

        private Font GetFont(string name, int size,FontStyle style)
        {
            return new Font(GetFontFamily(name), size, style);
        }

我提供字体名称的地方,它会找到Sentinel-SemiboldItalic.ttf字体。作为FontStyle,我尝试提供.NET框架中的任何选项(常规,粗体,斜体,下划线和删除线)。

无论如何,都会出现以下错误:

字体'Sentinel Semibold'不支持样式'Regular'。

我该怎么办?如何在C#中使用半粗体字体作为字体?另外,我能以某种方式将TrueType字体转换为常规字体吗(如果这样可以解决问题)?

c# .net fonts true-type-fonts
2个回答
2
投票

您可以读到this question的答案,每种字体都有其自己的属性(例如:是否启用Regular Style,如字体创建者所提供。

通过查看字体(甚至仅通过查看其名称),似乎很明显,它是一种子类型,其定义特征恰好是(半)粗体和斜体;因此,选择不删除这些功能确实很合逻辑。如果要使用非粗体,非斜体版本,则应依赖父系(Sentinel)。


0
投票

我也像你一样面临同样的问题。并发现即使我们添加了font [.ttf]文件,也不正确的字体也会添加到privatefontcollection中。因此,我找到了这种情况的解决方法。此问题将被随机抛出。因此,请添加字体,直到在专用字体集合中添加了正确的字体为止。

  private FontFamily GetFontFamily(string name)
    {
        PrivateFontCollection pfc = new PrivateFontCollection();
        var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
        pfc.AddFontFile(path);
**while (pfc.FontFamilies != null && pfc.Families.Length > 0 && (pfc.FontFamilies[0] as FontFamily).Name != "YourFontName")
        {
            pfc.Dispose();
            pfc = new PrivateFontCollection();
            GetFontFamily();
        }
        return pfc.Families[0];
    }
© www.soinside.com 2019 - 2024. All rights reserved.