Search code examples
c#.netexceptioniconssystem.drawing

System.Drawing.Icon constructor throwing "Operation completed successfully" exception


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.


Solution

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