Learn how to easily extract the icon from any executable with C#.

I know, it's weird to end up doing this to obtain the icon of an application that you can't find on the internet, pretty weird. However, as the last resource, this is just great! After googling multiple ways of how to extract the icon of any executable in Windows, there were a lot of licensed applications that would help the user to do this, I ended up again writing my own solution for this. In this case, I was trying to find the icon of PuTTYGen.

In this article, I'm going to explain to you how to easily obtain the icon (file .ico) of any executable in Windows using some C# in WinForms.

Extracting the Icon with ExtractAssociatedIcon

The drawing classes of .NET allows you to obtain the icon from a specific executable, specifically in the icon class. When the ExtractAssociatedIcon method is used with bitmaps, a thumbnail image may be returned instead of an icon, only if the system that is running the application has a registry setting that causes the bitmap files to be shown as thumbnail images.

The following helper method will do the trick catching any possible exception in your code:

// Import the Drawing namespace
using System.Drawing;

/// <summary>
/// Returns an icon representation of an image that is contained in the specified file.
/// </summary>
/// <param name="executablePath"></param>
/// <returns></returns>
public static Icon ExtractIconFromFilePath(string executablePath)
{
    Icon result = (Icon) null;

    try
    {
        result = Icon.ExtractAssociatedIcon(executablePath);
    }
    catch (Exception)
    {
        Console.WriteLine("Unable to extract the icon from the binary");
    }

    return result;
}

You can do whatever you need with the icon with this method.

Saving the icon

In our case, we only need to save the icon as a file in the system to convert it later to PNG. Icon objects need however to be saved using a FileStream, for example, to obtain the icon from PuTTYgen:

// 1. Specify the absolute path of the executable
string executablePath = @"C:\Program Files\PuTTY\puttygen.exe";

// 2. Store the icon instance
Icon theIcon = ExtractIconFromFilePath(executablePath);

// 3. If the icon was extracted, proceed to save it
if (theIcon != null)
{
    // 4. Save the icon to my desktop 
    using (FileStream stream = new FileStream(@"C:\Users\sdkca\Desktop\puttygen.ico", FileMode.CreateNew))
    {
        theIcon.Save(stream);
    }
}

The extracted icon of Puttygen is:

Puttygen Icon

Great isn't it?

Full example

The following class shows a basic example of how to use it when triggering the extraction of the icon to a button click:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace Sandbox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string executablePath = @"C:\Program Files\PuTTY\puttygen.exe";

            Icon theIcon = ExtractIconFromFilePath(executablePath);

            if (theIcon != null)
            {
                // Save it to disk, or do whatever you want with it.
                using (FileStream stream = new FileStream(@"C:\Users\sdkca\Desktop\myfile.ico", FileMode.CreateNew))
                {
                    theIcon.Save(stream);
                }
            }
        }

        /// <summary>
        /// Returns an icon representation of an image that is contained in the specified file.
        /// </summary>
        /// <param name="executablePath"></param>
        /// <returns></returns>
        public static Icon ExtractIconFromFilePath(string executablePath)
        {
            Icon result = (Icon) null;

            try
            {
                result = Icon.ExtractAssociatedIcon(executablePath);
            }
            catch (Exception)
            {
                Console.WriteLine("Unable to extract the icon from the binary");
            }

            return result;
        }
    }
}

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors