[英]Font doesn't exist and crashes the .NET application, need to check for or install the font
部署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());
}
將字體作為資源嵌入,並在顯示任何UI元素之前檢查/安裝它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.