Click here to Skip to main content
Licence CPOL
First Posted 9 Jun 2008
Views 44,686
Downloads 1,616
Bookmarked 79 times

Extract icons from EXE or DLL files

By Tsuda Kageyu | 2 Sep 2008
Extract all the variations of an icon, including the ones ExtractIconEx() can't extract.
   4.69 (25 votes)
1 vote, 4.0%
1

2
1 vote, 4.0%
3
4 votes, 16.0%
4
19 votes, 76.0%
5
4.69/5 - 25 votes
1 removed
μ 4.59, σa 1.63 [?]
 

IconExtractorApp

Introduction

The class I introduce in this article is for extracting an icon from an EXE or DLL file. Though it's common to use the ExtractIconEx() Win32 API function for this purpose, ExtractIconEx() can't extract all the variations of an icon.

I had faced the problem, and made a class 'IconExtractor' as the solution to the problem.

Using the code

To use this class, instantiate it first with the filename from which you want to extract the icons. You should dispose it after use.

Then, call the GetIcon() method with the index of the icon you want, to extract the specified icon. The index is same as the nIconIndex for ExtractIconEx(). This method returns a System.Drawing.Icon object containing all the variations in size and color depth. You can use it for Form.Icon, NotifyIcon.Icon, etc. The proper variation will be selected by the system.

Use the SplitIcon() static method if you want to split the variations into individual instances. This method returns an array of icons, each element containing one of the variations.

using TKageyu.Utils;

...

using (IconExtractor ie = new IconExtractor(@"D:\sample.exe")) 
{
    Icon icon0 = ie.GetIcon(0);
    Icon icon1 = ie.GetIcon(1);

    ...
    
    Icon[] splitIcons = IconExtractor.SplitIcon(icon0);

    ...
}

How it works

The icons and other materials are embedded in the executable file in binary form. Those pieces of binary data are called 'resources'. They can't be read directly with .NET classes, because they are different from the managed resources that the .NET Framework can handle. They should be read with Win32 API functions such as FindResource(), LoadResource() etc.

This class reads binary data by using the functions in the GetResourceData() private method. The parameters given to the method are collected when the class is instantiated. The binary data is in similar form to an .ico file, and can be converted easily to be equivalent to an .ico file. It is given to the icon constructor after conversion, and will be parsed as read from an .ico file. The conversion is done in the CreateIcon() method.

License

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

About the Author

Tsuda Kageyu

Software Developer

Japan Japan

Member
In 1985, I got my first computer Casio MX-10, the cheapest one of the MSX computers. Then I began programming in BASIC and assembly language, and have experienced over ten languages from that time on.
Now, my primary languages are C++ and C#. Working for a small company in my home town, the countryside of Japan.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood PinmemberCarlos Ernesto Fuentes Rasique5:18 17 Dec '11  
GeneralMy vote of 5 Pinmemberjoehoper4:48 20 Nov '11  
AnswerThanks Pinmemberpahancs17:06 13 Oct '11  
QuestionIconExtractor Program PinmemberScott Caryer10:24 26 Jul '11  
AnswerRe: IconExtractor Program PinmemberAbbydonKrafts10:40 31 Jul '11  
QuestionWould not work with Windows 7 64 bit OS machine Pinmemberelderdo14:02 6 Jul '11  
AnswerRe: Would not work with Windows 7 64 bit OS machine PinmemberAbbydonKrafts10:35 31 Jul '11  
GeneralUnable to Convert 256x256 Icons from Microsoft Applications to Bitmap Pinmemberjohndotthinix5:34 1 Dec '10  
QuestionHow to determine if DLL or EXE has Icons? Pinmemberf r i s c h23:10 11 Feb '10  
GeneralThanx! PinmemberMuammar©23:09 12 Sep '08  
GeneralGood work but this way has already been taken... PinmemberGuillaume Leparmentier3:45 11 Jun '08  
GeneralRe: Good work but this way has already been taken... PinPopularmemberTsuda Kageyu11:43 11 Jun '08  
GeneralNice PinmemberSaurabh.Garg6:18 10 Jun '08  
GeneralRe: Nice PinmemberTsuda Kageyu11:45 10 Jun '08  
GeneralNice Pinmemberholy_spirit5:12 10 Jun '08  
AnswerRe: Nice PinmemberTsuda Kageyu11:42 10 Jun '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120217.1 | Last Updated 2 Sep 2008
Article Copyright 2008 by Tsuda Kageyu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid