public unsafe static Icon ExtractIconFromEXE(string file, bool large) { int readIconCount = 0; IntPtr[] hDummy = new IntPtr[1] { IntPtr.Zero }; IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero }; try { if (large) { readIconCount = (int)Icons.ExtractIconEx(file, 0, hIconEx, hDummy, 1); } else { readIconCount = (int)Icons.ExtractIconEx(file, 0, hDummy, hIconEx, 1); } if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero) { Icon extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone(); return(extractedIcon); } else { return(null); } } catch (Exception ex) { throw new ApplicationException("Could not extract icon", ex); } finally { foreach (IntPtr ptr in hIconEx) { if (ptr != IntPtr.Zero) { Icons.DestroyIcon(ptr); } } foreach (IntPtr ptr in hDummy) { if (ptr != IntPtr.Zero) { Icons.DestroyIcon(ptr); } } } }
private Icon GetIcon(string fileName, int iconID) { try { IntPtr hc = Icons.ExtractIcon(this.Handle, fileName, iconID); if (!hc.Equals(IntPtr.Zero)) { return(Icon.FromHandle(hc)); } } catch { } return(null); }
private Icon SHGetFileIcon(string fileName, int iconID, bool large) { Icons.SHFILEINFO info = new Icons.SHFILEINFO(); int cbFileInfo = Marshal.SizeOf(info); Icons.SHGFI flags; if (large) { flags = Icons.SHGFI.Icon | Icons.SHGFI.LargeIcon | Icons.SHGFI.UseFileAttributes; } else { flags = Icons.SHGFI.Icon | Icons.SHGFI.SmallIcon | Icons.SHGFI.UseFileAttributes; } Icons.SHGetFileInfo(fileName, 0x00000100, out info, (uint)cbFileInfo, flags); return(Icon.FromHandle(info.hIcon)); }