29

I can get the executable location from the process, how do I get the icon from file?

Maybe use windows api LoadIcon(). I wonder if there is .NET way...

Haim Bender
  • 7,937
  • 10
  • 53
  • 55
  • See also, almost a duplicate: http://stackoverflow.com/questions/3873356/where-is-a-net-application-icon-stored – Doc Brown May 06 '16 at 07:45

3 Answers3

43
Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
  • 3
    `Process.MainModule` can throw `Win32Exception`s on system processes or between 32-bit and 64-bit processes. See [this question](http://stackoverflow.com/questions/9501771/how-to-avoid-a-win32-exception-when-accessing-process-mainmodule-filename-in-c) for details. – Nathan Goings Mar 19 '14 at 04:44
  • 1
    Also, my icons have no transparency giving ugly black outlines :/ – Nathan Goings Mar 19 '14 at 04:45
13

This is a sample from a console application implementation.

using System;
using System.Drawing;         //For Icon
using System.Reflection;      //For Assembly

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Gets the icon associated with the currently executing assembly
                //(or pass a different file path and name for a different executable)
                Icon appIcon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);                
            }
            catch(ArgumentException ae) 
            {
                //handle
            }           
        }
    }
}
Carsten
  • 11,287
  • 7
  • 39
  • 62
RobS
  • 9,382
  • 3
  • 35
  • 63
  • Correction, that code does it for the current assembly... not related. oh, and you're missing parens. – TheSoftwareJedi Oct 15 '08 at 02:29
  • Instead of using reflection for current assembly, wouldn't it be faster to use `Icon = Icon.ExtractAssociatedIcon(Process.GetCurrentProcess().MainModule.FileName);`? – Igor Levicki Mar 24 '22 at 12:10
3

Use the ExtractIconEx (and here) p/invoke. You can extract small and large icons from any dll or exe. Shell32.dll itself has over 200 icons that are quite useful for a standard Windows application. You just have to first figure out what the index is for the icon(s) you want.

Edit: I did quick SO search and found this. The index 0 icon is the application icon.

Community
  • 1
  • 1
Bob Nadler
  • 2,755
  • 24
  • 20