Click here to Skip to main content
15,860,861 members
Articles / Desktop Programming / Windows Forms

Extracting Icons from EXE/DLL and Icon Manipulation

Rate me:
Please Sign up or sign in to vote.
4.83/5 (37 votes)
17 Jan 2009CPOL2 min read 153.7K   11K   84   24
How to extract icons from EXE/DLL, split/merge icons, and get icons associated with files.

Image 1

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:

C#
using TAFactory.IconPack;

Use the IconHelper class to obtain the icons as follows:

C#
//Get the open folder icon from shell32.dll.
Icon openFolderIcon = IconHelper.ExtractIcon(@"%SystemRoot%\system32\shell32.dll", 4);
//Get all icons contained in shell32.dll.
List<icon> shellIcons = IconHelper.ExtractAllIcons(@"%SystemRoot%\system32\shell32.dll");
//Split the openFolderIcon into its icon images.
List<icon> openFolderSet = IconHelper.SplitGroupIcon(openFolderIcon);
//Get the small open folder icon.
Icon smallFolder = IconHelper.GetBestFitIcon(openFolderIcon, 
                              SystemInformation.SmallIconSize);
//Get large icon of c drive. 
Icon largeCDriveIcon = IconHelper.GetAssociatedLargeIcon(@"C:\");
//Get small icon of c drive. 
Icon smallCDriveIcon = IconHelper.GetAssociatedSmallIcon(@"C:\");
//Merge icon images in a single icon.
Icon cDriveIcon = IconHelper.Merge(smallCDriveIcon, largeCDriveIcon);
//Save the icon to a file.
FileStream fs = File.Create(@"c:\CDrive.ico");
cDriveIcon.Save(fs);
fs.Close();

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect RoboteQ
Egypt Egypt
Abdallah Gomah
Master in Computer Science, 2013, Faculty of Computers & Information, Cairo University (Egypt)

- Working as a developer since I graduated from the faculty.
- Like coding in C++ aloso like to code in assembly.
- Have a great experience in coding with .NET (C#/VB), but I prefer the C# notation.
- Had written a lot of applications Desktop and Web.

I love playing football as much as I love the programming.

Comments and Discussions

 
GeneralThanks for Extracting-Icons-from-EXE-DLL-and-Icon-Manipulatio Pin
spvasekar11-Apr-19 16:37
spvasekar11-Apr-19 16:37 
QuestionLoading .ICL Icon Conainer into IconListview Pin
rojaldearintok24-Nov-13 17:12
rojaldearintok24-Nov-13 17:12 
QuestionSuper Code But Pin
Member 985556517-May-13 6:29
Member 985556517-May-13 6:29 
GeneralThanks. Pin
Fatih Nehir30-Nov-12 3:07
Fatih Nehir30-Nov-12 3:07 
QuestionWhere does TAFactory.IconPack.dll come from Pin
Jradxl26-Oct-12 11:38
Jradxl26-Oct-12 11:38 
AnswerRe: Where does TAFactory.IconPack.dll come from Pin
Abdallah Gomah26-Oct-12 10:42
Abdallah Gomah26-Oct-12 10:42 
QuestionLittle help needed please Pin
Member 89109501-May-12 5:00
Member 89109501-May-12 5:00 
GeneralMy vote of 5 Pin
Mario Majčica19-May-11 22:47
professionalMario Majčica19-May-11 22:47 
GeneralMy vote of 5 Pin
Alex Essilfie30-Dec-10 22:59
Alex Essilfie30-Dec-10 22:59 
GeneralMy vote of 5 Pin
playhere23-Aug-10 23:24
playhere23-Aug-10 23:24 
General.Net 2.0 equivalent of icon extraction Pin
oguzhanFilizlibay()29-Mar-10 3:22
oguzhanFilizlibay()29-Mar-10 3:22 
GeneralRe: .Net 2.0 equivalent of icon extraction Pin
playhere23-Aug-10 21:47
playhere23-Aug-10 21:47 
GeneralLeak Pin
Sabrina Hope23-Feb-10 6:01
Sabrina Hope23-Feb-10 6:01 
I think there is a leak in your code so I've modified the following code:

        public static Icon GetAssociatedIcon(string fileName, IconFlags flags)<br />
        {<br />
            flags |= IconFlags.Icon;<br />
            SHFILEINFO fileInfo = new SHFILEINFO();<br />
            IntPtr result = Win32.SHGetFileInfo(fileName, 0, ref fileInfo, (uint)Marshal.SizeOf(fileInfo), (SHGetFileInfoFlags) flags);<br />
<br />
            if (fileInfo.hIcon == IntPtr.Zero)<br />
                return null;<br />
<br />
            Icon ico = (Icon)Icon.FromHandle(fileInfo.hIcon).Clone();<br />
<br />
            Win32.DestroyIcon(fileInfo.hIcon);<br />
<br />
            return ico;<br />
        }


Thank to Paul Ingles for this trick : Obtaining (and managing) file and folder icons using SHGetFileInfo in C#[^]
GeneralThanks Pin
lewebus21-Jan-10 5:22
lewebus21-Jan-10 5:22 
GeneralNice article, but got error Pin
Rick Hansen21-Jan-09 4:23
Rick Hansen21-Jan-09 4:23 
GeneralRe: Nice article, but got error Pin
Abdallah Gomah22-Jan-09 5:45
Abdallah Gomah22-Jan-09 5:45 
GeneralRe: Nice article, but got error Pin
FrozenCow_13-Feb-09 5:54
FrozenCow_13-Feb-09 5:54 
GeneralRe: Nice article, but got error [modified] Pin
playhere23-Aug-10 23:08
playhere23-Aug-10 23:08 
GeneralGreat Article Pin
Swapnil Shah18-Jan-09 20:57
Swapnil Shah18-Jan-09 20:57 
GeneralVista Icons Pin
FrozenCow_17-Jan-09 11:55
FrozenCow_17-Jan-09 11:55 
GeneralRe: Vista Icons Pin
Abdallah Gomah18-Jan-09 8:25
Abdallah Gomah18-Jan-09 8:25 
AnswerRe: Vista Icons Pin
Alex Essilfie30-Dec-10 22:55
Alex Essilfie30-Dec-10 22:55 
GeneralVery Good Abdallah You are the best Pin
nonoee201017-Jan-09 10:38
nonoee201017-Jan-09 10:38 
Generalgood job! Pin
Southmountain17-Jan-09 6:41
Southmountain17-Jan-09 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.