2

I use Imaging.CreateBitmapSourceFromHIcon method to convert Icon to BitmapSource:

private static System.Windows.Media.ImageSource loadWpfImageSource(Icon icon, Size size)
    {
        if (icon != null)
        {
            return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }

        return null;
    }

At one of my client's computer Imaging.CreateBitmapSourceFromHIcon throws the next COMException:

System.Runtime.InteropServices.COMException (0x8007057A): Wrong pointer descriptor. (Exception from HRESULT: 0x8007057A)
at System.Windows.Interop.InteropBitmap..ctor(IntPtr hicon, Int32Rect sourceRect, BitmapSizeOptions sizeOptions)
at System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(IntPtr icon, Int32Rect sourceRect, BitmapSizeOptions sizeOptions)

Any ideas why could this happen? (And how to fix it)

Artem Kachanovskyi
  • 1,839
  • 2
  • 18
  • 27
  • 1
    The error message is wrong. This is a low-level Windows error, error code 1402, ERROR_INVALID_CURSOR_HANDLE, "Invalid cursor handle". Icons and cursors are almost indistinguishable. A simple explanation is that the icon is corrupted or invalid, that can happen of course. – Hans Passant Apr 24 '14 at 12:57
  • 1
    @Hans Passant: if an icon is corrupted, does it mean that if I convert `Icon` through the way that uses `MemoryStream`, the `Icon` will fail to convert anyway? – Artem Kachanovskyi Apr 24 '14 at 13:02

0 Answers0