Arial Black Italic字体导致WinForms应用程序崩溃。

3
我维护的一个WinForms应用程序在极小部分用户的机器上崩溃(可能是到目前为止大约4个)。 对于那些用户,应用程序每次都会崩溃,并且在第一个对话框被显示之前就会崩溃。 异常
Source:
System.Drawing

Message:
Font 'Arial Black' does not support style 'Bold'.

Stack Trace:
at System.Drawing.Font.CreateNativeFont()
at System.Drawing.Font.Initialize(FontFamily family, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
at System.Drawing.Font.Initialize(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet, Boolean gdiVerticalFont)
at System.Drawing.Font..ctor(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte gdiCharSet)

应用程序使用的字体之一是Arial Black:

this.label3.Font = new System.Drawing.Font("Arial Black", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

第一次发生这种崩溃时,我注意到用户计算机上有一个字体,而我的电脑上没有。它叫做“Arial Black Italic”,日期是1997年,文件名为“ARBLI___.TTF”。如图所示:enter image description here用户使用的是Windows XP。
删除该字体后,应用程序正常运行。在过去的22个月中,大约有3个用户遇到了这个问题。每次从用户的计算机中删除“Arial Black Italic”字体似乎可以解决问题。
最近一次出现这种崩溃,用户使用的是Windows 7,字体日期更新得多,但前述协议仍然解决了问题。
现在,我正在尝试找出这个崩溃错误的根本原因,并想方设法防止它发生。
1个回答

0

尝试类似这样的东西。

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // Create regular font first.
            // Depending on the user's system, this font may already be bold.
            //

            var theFont = new System.Drawing.Font(
                "Arial Black",
                8.25F,
                System.Drawing.FontStyle.Regular,
                System.Drawing.GraphicsUnit.Point,
                ( ( byte )( 0 ) )
                );

            // If font is not bold, then try to create it.
            //

            if ( ( null != theFont ) && !theFont.Bold )
            {
                if ( theFont.FontFamily.IsStyleAvailable( FontStyle.Bold ) )
                {
                    theFont = new Font( theFont, FontStyle.Bold );
                }
            }

            // Now use the font.
            //

            this.label3.Font = theFont;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接