The System.Drawing.Icon constructor throws an exception "Operation completed successfully"

On a Windows XP-based computer, the following code throws a System.ComponentModel.Win32Exception message with the message "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.

Full stack trace

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"

This line 50 is the source line I posted.

This is a WPF application, and on a computer running Windows 7, the code works fine.

EDIT: The icon turned out to not work at all in Windows XP, adding that 256 color versions fixed it.

+5
source share
4 answers

Windows XP, 256 , , .

+1

-, , . , , , , , using , IDisposable.

, , - :

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

.

+1

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!
}

, , , .

+1

I had a similar problem. in my case, the icon file was a multi-contact file containing 32x32, 48x48 and 256x256 sized icons. I changed it to one 32x32 icon file size and after that it worked fine.

0
source

All Articles