Icon getIcon(bool large) { // Get icon index and path: var iconIndex = 0; var iconPath = new StringBuilder(260, 260); if (linkA == null) { linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } else { linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } var iconFile = iconPath.ToString(); // If there are no details set for the icon, then we must use // the shell to get the icon for the target: if (iconFile.Length == 0) { // Use the FileIcon object to get the icon: var flags = FileIcon.SHGetFileInfoConstants.SHGFI_ICON | FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES; if (large) { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON; } else { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON; } var fileIcon = new FileIcon(Target, flags); return(fileIcon.ShellIcon); } // Use ExtractIconEx to get the icon: var hIconEx = new IntPtr[1] { IntPtr.Zero }; var iconCount = 0; if (large) { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, hIconEx, null, 1); } else { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, null, hIconEx, 1); } // If success then return as a GDI+ object Icon icon = null; if (hIconEx[0] != IntPtr.Zero) { icon = Icon.FromHandle(hIconEx[0]); //UnManagedMethods.DestroyIcon(hIconEx[0]); } return(icon); }
Icon getIcon(bool large) { // Get icon index and path: var iconIndex = 0; var iconPath = new StringBuilder(260, 260); if (linkA == null) linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); else linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); var iconFile = iconPath.ToString(); // If there are no details set for the icon, then we must use // the shell to get the icon for the target: if (iconFile.Length == 0) { // Use the FileIcon object to get the icon: var flags = FileIcon.SHGetFileInfoConstants.SHGFI_ICON | FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES; if (large) flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON; else flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON; var fileIcon = new FileIcon(Target, flags); return fileIcon.ShellIcon; } // Use ExtractIconEx to get the icon: var hIconEx = new IntPtr[1] {IntPtr.Zero}; var iconCount = 0; if (large) { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, hIconEx, null, 1); } else { iconCount = UnManagedMethods.ExtractIconEx( iconFile, iconIndex, null, hIconEx, 1); } // If success then return as a GDI+ object Icon icon = null; if (hIconEx[0] != IntPtr.Zero) { icon = Icon.FromHandle(hIconEx[0]); //UnManagedMethods.DestroyIcon(hIconEx[0]); } return icon; }