C# Programming > Drawing GDI+

C# Icon Extractor

c# icon

C# Application Icons

Professional applications written in any language commonly have an icon image to distinguish the program. It can be important for C# applications to extract those default icons from executables. For example picture a program manager that keeps track of program locations, representing each program with its own icon is essential.

Using a couple API calls you can easily extract icons from executable files using C#.

API C# Functions

You will need two API functions:

ExtractIconExA - Accesses the icon information on an executable file
DestroyIcon - Cleans up the memory by deleting the extracted information

C# Basics

The basic C# routine to extract an application icon is simple:

IntPtr largeIcon = IntPtr.Zero;
IntPtr smallIcon = IntPtr.Zero;
ExtractIconExA(path, 0, ref largeIcon, ref smallIcon, 1);

Where path is the location of the executable file. The advantage of ExtractIconEx is that it lets you extract two different size versions of the icon, which most applications have.

Converting the Icon

The result however will be a IntPtr. To convert IntPtr to Icon, which is a friendlier C# format, it takes one line:

Icon ico = Icon.FromHandle(largeIcon);

If you want to use that Icon as an Image or Bitmap, add:

Bitmap bmp = ico.ToBitmap();

Conclusion

The sample C#.NET application included at the bottom of the page is a utility that extracts the default icon from an executable. The source code contains an independent IconExtractor class that can be copied-and-pasted into your own .NET projects...

Back to C# Article List