微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

嵌入式字体在 Windows 7 中不支持“常规”样式,但在 Windows 10 中支持

如何解决嵌入式字体在 Windows 7 中不支持“常规”样式,但在 Windows 10 中支持

我在我的 .Net 4.5 软件中使用了一些嵌入字体。当我在 Windows 10 上运行软件时一切正常,但是当我尝试在 Windows 7 虚拟机上测试该软件时,它抛出了这个异常:

字体 'B Koodak' 不支持样式 'Regular'。

以下是我从资源加载嵌入字体的方法

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont,uint cbFont,IntPtr pdv,[System.Runtime.InteropServices.In] ref uint pcFonts);

public readonly PrivateFontCollection privateFontCollection = new PrivateFontCollection();

public readonly FontFamily Samim;

private readonly FontFamily IranNastaliq;

private readonly FontFamily BKoodak;

private readonly FontFamily BNazanin;

public MainForm()
{
    uint temp = 0;
    byte[][] fontData = new byte[][] { Properties.Resources.Samim,Properties.Resources.IranNastaliq_Web,Properties.Resources.BNazanin,Properties.Resources.BKoodak };

    for (int i = 0; i < fontData.Length; i++)
    {
        IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData[i].Length);
        System.Runtime.InteropServices.Marshal.copy(fontData[i],fontPtr,fontData[i].Length);
        privateFontCollection.AddMemoryFont(fontPtr,fontData[i].Length);
        AddFontMemResourceEx(fontPtr,(uint)fontData[i].Length,IntPtr.Zero,ref temp);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
    }

    Samim = privateFontCollection.Families[3];
    IranNastaliq = privateFontCollection.Families[2];
    BNazanin = privateFontCollection.Families[1];
    BKoodak = privateFontCollection.Families[0];


    InitializeComponent();

    Application.CurrentCulture = new CultureInfo("fa-IR");
    
    // Other codes...
}

private void Draw(ref graphics)
{
    Font font = new Font(BKoodak,18);
    graphics.DrawString("سلام",font,new SolidBrush(Color.Black),new StringFormat());
}

为什么在 Windows 10 上可以,但在 Windows 7 上不行?我该如何解决


编辑
我发现这个字体只支持粗体和轮廓模式。在 Microsoft Word 中,此字体的粗体和常规样式没有区别。
所以我将 Font font = new Font(BKoodak,18); 改为 Font font = new Font(BKoodak,18,FontStyle.Bold);,现在它可以在 Windows 7 上运行了。

现在的问题是:尽管此字体不支持常规样式,但为什么常规样式在 Windows 10 中可以正常工作?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。