System.Drawing.Icon 构造函数抛出“操作成功完成”例外
在 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 色版本似乎已修复它。
On a Windows XP machine, the following code is throwing a System.ComponentModel.Win32Exception with the message "The operation completed successfully"
System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico");
I can stop the program crashing with
try
{
System.Drawing.Icon icon = new System.Drawing.Icon("icon.ico");
}
catch(System.ComponentModel.Win32Exception ex)
{
if (ex.NativeErrorCode != 0)
{
throw;
}
}
but of course the icon is not set.
The full stack trace is
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"
That line 50 is the original line I posted.
This is a WPF app, and on a Windows 7 machine the code works fine.
EDIT: Turned out the icon wasn't working in Windows XP at all, adding 256 colour versions seems to have fixed it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从表面上看,问题似乎是没有正确处理物体的问题。很难准确指出您的情况中问题发生的位置,但作为一般经验法则,请确保在处理实现
IDisposable
的对象时实现using
指令。即使在您提供的示例中,也尝试执行以下操作:
阅读这篇 文章。
From the looks of it the problem seems to be an issue with not disposing with objects properly. It's hard to pin point exactly where the issue in your case is occurring but as a general rule of thumb make sure you implement the
using
directive when dealing with objects that implementIDisposable
.Even in the sample you have provided try doing something like:
Have a read of this article.
文件
icon1.ico
是否与 .NET 可执行文件位于同一目录中?您没有明确说明...您是否将其作为外部图标文件来阅读?也许这希望这有帮助,
此致,
汤姆.
Does the file
icon1.ico
exists in the same directory as the .NET executable? You did not say explicitly...are you reading this as an external icon file? perhaps thisHope this helps,
Best regards,
Tom.
我有类似的问题。就我而言,图标文件是一个包含 32x32、48x48 和 256x256 大小图标的多图标文件。我将其更改为单个图标文件大小 32x32,之后工作正常。
I had a similar problem. in my case the icon file was a multiicon file containing 32x32, 48x48 and 256x256 size icons. I changed it to a single icon file size 32x32 and it worked fine after that.
结果发现该图标在 Windows XP 中根本不起作用,添加 256 色版本似乎已经修复了它。
Turned out the icon wasn't working in Windows XP at all, adding 256 colour versions seems to have fixed it.