在 Windows XP 机器上,以下代码抛出 System.ComponentModel.Win32Exception 并显示消息“操作成功完成”
System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico");
我可以阻止程序崩溃
try
{
System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico");
}
catch(System.ComponentModel.Win32Exception ex)
{
if (ex.NativeErrorCode != 0)
{
throw;
}
}
但当然图标没有设置。
完整的堆栈跟踪是
at System.Drawing.Icon.Initialize(Int32 width, Int32 height)
at System.Drawing.Icon..ctor(String fileName, Int32 width, Int32 height)
at System.Drawing.Icon..ctor(String fileName)
at hermes.Window1..ctor() in D:\\projects\\hermesclient\\hermesWPF\\hermes\\Window1.xaml.cs:line 50"
第 50 行是我发布的原始行。
这是一个 WPF 应用程序,在 Windows 7 机器上代码运行良好。
编辑:原来图标在 Windows XP 中根本不起作用,添加 256 色版本似乎已经修复它。