System.Drawing.Icon 构造函数抛出“操作成功完成”例外

发布于 2024-08-23 12:27:07 字数 873 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晌融 2024-08-30 12:27:07

从表面上看,问题似乎是没有正确处理物体的问题。很难准确指出您的情况中问题发生的位置,但作为一般经验法则,请确保在处理实现 IDisposable 的对象时实现 using 指令。

即使在您提供的示例中,也尝试执行以下操作:

using (var icon = new System.Drawing.Icon("icon.ico"))
{
    // use icon
}
// icon is then disposed.

阅读这篇 文章

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 implement IDisposable.

Even in the sample you have provided try doing something like:

using (var icon = new System.Drawing.Icon("icon.ico"))
{
    // use icon
}
// icon is then disposed.

Have a read of this article.

自找没趣 2024-08-30 12:27:07

文件 icon1.ico 是否与 .NET 可执行文件位于同一目录中?您没有明确说明...您是否将其作为外部图标文件来阅读?也许这

string sPath2Icon = Path.Combine(Environment.CurrentDirectory, "icon1.ico");
using (System.Drawing.Icon icon = new System.Drawing.Icon(sPath2Icon)){
    // Do what you have to do with icon!
}

希望这有帮助,
此致,
汤姆.

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 this

string sPath2Icon = Path.Combine(Environment.CurrentDirectory, "icon1.ico");
using (System.Drawing.Icon icon = new System.Drawing.Icon(sPath2Icon)){
    // Do what you have to do with icon!
}

Hope this helps,
Best regards,
Tom.

情定在深秋 2024-08-30 12:27:07

我有类似的问题。就我而言,图标文件是一个包含 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.

天冷不及心凉 2024-08-30 12:27:07

结果发现该图标在 Windows XP 中根本不起作用,添加 256 色版本似乎已经修复了它。

Turned out the icon wasn't working in Windows XP at all, adding 256 colour versions seems to have fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文