Abstract
This article describes how to extract icons from an executable module (EXE or DLL), and also how to get the icons associated with a file.
In this article, you will find how to get the icon image that best fits the size you want to display. You can also find how to split an icon file to get its image.
Introduction
Icons are a varied lot—they come in many sizes and color depths. A single icon resource—an ICO file, or an icon resource in an EXE or DLL file—can contain multiple icon images, each with a different size and/or color depth.
Windows extracts the appropriate size/color depth image from the resource depending on the context of the icon's use. Windows also provides a collection of APIs for accessing and displaying icons and icon images.
The code I introduce will help you extract icons from executable modules (EXE, DLL) without the need to know the Windows APIs that are used in this situation.
The code will also help you in extracting specific icon images from an icon file, and will help you in extracting the icon image that best fits a supplied icon size.
Background
If you want to understand what is going on in this code, you should know how to call APIs from C# code. You also need to know the icon format, about which you will find here: MSDN.
Using the code
First, you need to add a reference to TAFactory.IconPack.dll, or add the project named IconPack to your project.
Add the following statement to your code:
using TAFactory.IconPack;
Use the IconHelper
class to obtain the icons as follows:
Icon openFolderIcon = IconHelper.ExtractIcon(@"%SystemRoot%\system32\shell32.dll", 4);
List<icon> shellIcons = IconHelper.ExtractAllIcons(@"%SystemRoot%\system32\shell32.dll");
List<icon> openFolderSet = IconHelper.SplitGroupIcon(openFolderIcon);
Icon smallFolder = IconHelper.GetBestFitIcon(openFolderIcon,
SystemInformation.SmallIconSize);
Icon largeCDriveIcon = IconHelper.GetAssociatedLargeIcon(@"C:\");
Icon smallCDriveIcon = IconHelper.GetAssociatedSmallIcon(@"C:\");
Icon cDriveIcon = IconHelper.Merge(smallCDriveIcon, largeCDriveIcon);
FileStream fs = File.Create(@"c:\CDrive.ico");
cDriveIcon.Save(fs);
fs.Close();
References