如果代码在非 UI 应用程序中执行,GDI 将我的字体更改为 MS Sans Serif

问题描述

我有一种使用 DrawString() 生成图像的方法。如果我在我从任何带有 UI 的程序或控制台应用程序测试的每台机器上调用方法,一切都很好。图片显示了正确的字体。

但如果我从 Windows 服务或 IIS 网站调用方法,GDI (?) 会自动更改为 Microsoft Sans Serif,无一例外。

它在 Windows 7 上运行多年。更改为 Windows Server 2019 后第一次出现问题。现在我有 2 台带有 Windows 10 Build 20H2 和相同 .NET 版本的开发机器。在一台机器上生成图像效果很好,在另一台机器上字体会发生变化。任何想法,我能做什么,让它在每台机器上工作?

public void GenerateBitmap()
{
    string drawString = "Sample Text 1234567890";

    using  (Bitmap bmp = new Bitmap(5000,200,PixelFormat.Format32bppArgb))
    {
        bmp.SetResolution(300,300);
        using (Graphics grfx = Graphics.FromImage(bmp))
        {
            using (Brush brush = new SolidBrush(Color.Black))
            {
                Font f = new Font("MyFontName",22F);
                grfx.DrawString(drawString,f,brush,new Point(0,0));
            }
        }
        bmp.Save("test.png",System.Drawing.Imaging.ImageFormat.Png);
        bmp.dispose();
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)