gpt4 book ai didi

c# - 字体不存在并导致 .NET 应用程序崩溃,需要检查或安装字体

转载 作者:太空狗 更新时间:2023-10-29 23:25:28 26 4
gpt4 key购买 nike

部署 C# .NET 应用程序时,我们的 GUI 元素使用“Arial”字体(这是 Visual Studio GUI 设计器中的默认字体)。

由于某种原因,我们正在与之合作的一位特定客户没有安装 Arial 字体(他们必须手动删除它,因为据我所知,所有 Windows 安装都默认安装了它)。

这会导致异常/应用程序崩溃。

是否有某种方法可以确保 C# 中存在某种字体,并且/或如果不存在则自动安装它?

最佳答案

您需要将字体作为资源嵌入,然后执行与此类似的操作:

[DllImport("gdi32", EntryPoint = "AddFontResource")]
public static extern int AddFontResourceA(string lpFileName);

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

List<FontFamily> fontsFamilies = new List<FontFamily>(FontFamily.Families);
if (!fontsFamilies.Exists(f => f.Name.Equals("Arial")))
{
//Save the font from resource here....


//Install the font
int result = AddFontResourceA(@"C:\MY_FONT_LOCATION\Arial.TTF");
}

Application.Run(new Form1());
}

关于c# - 字体不存在并导致 .NET 应用程序崩溃,需要检查或安装字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9486935/

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