4

I tied SHGetFileInfo and ExtractIconEx, both return a normal 32x32 icon and 16x16 with only 16 colors, and it looks awful. How do I extract a full color icon?

My code

SHFILEINFO shinfo = new SHFILEINFO();
IntPtr hImgSmall = SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
Icon icon = (Icon)System.Drawing.Icon.FromHandle(shinfo.hIcon).Clone();
DestroyIcon(shinfo.hIcon);
Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
alx
  • 57
  • 1
  • 5
  • Are you sure that file you trying to extract small icon has full color 16x16 icon? – arbiter Jul 06 '09 at 10:49
  • At least the windows explorer shows it. http://img196.imageshack.us/i/iconsdys.png/ – alx Jul 06 '09 at 12:20
  • That is strange, it must work, at least it work in my case. You absolutely need Icon, or Image is also acceptable? – arbiter Jul 06 '09 at 12:58
  • I convert finally from Icon to Image as icon.ToBitmap(). The result you could see above. – alx Jul 06 '09 at 14:01

3 Answers3

3

Did you tried the following?

Icon LargeIcon = Icon.ExtractAssociatedIcon(fileName);
Icon SmallIcon = new Icon(LargeIcon, 16, 16);
arbiter
  • 9,447
  • 1
  • 32
  • 43
2

I tried this example link text and works.....got 16*16 with alpha channel. Try it.

Icebob
  • 1,132
  • 7
  • 14
  • Confirm this works. Seems you must call ToBitmap() else you will only get 16 colors! thanks –  Oct 11 '13 at 07:28
  • 1
    Wow, this is the only one that I can get to work. out of like the 20 I have found on google. This one rules! – Omzig Dec 28 '13 at 00:00
  • This is the reason why u should post the code not a link. The link is not valid anymore. – Deniz Apr 19 '23 at 14:30
1

You can find a working how-to here: http://support.microsoft.com/?scid=kb;en-us;319350&x=14&y=9

user350966
  • 19
  • 1