Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C# 4.0

Extract icons from EXE or DLL files

Rate me:
Please Sign up or sign in to vote.
4.91/5 (46 votes)
10 Nov 2014BSD3 min read 204.2K   13.7K   149  
Extract all the variations of an icon, including the ones ExtractIconEx() can't extract.
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Windows.Forms;

namespace SampleApp
{
    public class IconPickerDialog : CommonDialog
    {
        private static class NativeMethods
        {
            [DllImport("shell32.dll", EntryPoint = "#62", CharSet = CharSet.Unicode, SetLastError = true)]
            [SuppressUnmanagedCodeSecurity]
            public static extern bool SHPickIconDialog(
                IntPtr hWnd, StringBuilder pszFilename, int cchFilenameMax, out int pnIconIndex);
        }

        private const int MAX_PATH = 260;

        [DefaultValue(default(string))]
        public string FileName
        {
            get;
            set;
        }

        [DefaultValue(0)]
        public int IconIndex
        {
            get;
            set;
        }

        protected override bool RunDialog(IntPtr hwndOwner)
        {
            var buf = new StringBuilder(FileName, MAX_PATH);
            int index;

            bool ok = NativeMethods.SHPickIconDialog(hwndOwner, buf, MAX_PATH, out index);
            if (ok)
            {
                FileName = Environment.ExpandEnvironmentVariables(buf.ToString());
                IconIndex = index;
            }

            return ok;
        }

        public override void Reset()
        {
            FileName = null;
            IconIndex = 0;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
Japan Japan
In 1985, I got my first computer Casio MX-10, the cheapest one of MSX home 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 in a rural area of Japan.


Comments and Discussions