Warning: Invalid argument supplied for foreach() in /home/techrecipes/public_html/wp-content/themes/techrecipes/header.php on line 77

Load an Icon from an Embedded Resource in .NET

When you are developing a Windows Forms application in .NET, it’s not immediately obvious how to programatically load an icon file embedded in your executable. This recipe shows you the 1 line solution.


Your icon file should be a regular windows icon file. Add it to your project, and in the properties for the icon make sure that it is set to Embedded Resource.

Now in your code, add the following line:
Icon theIcon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.Filename.ico"));

Replace “MyNameSpace” with the namespace of your application, and replace “Filename” with the name of your icon file.

You should now be able to use the icon directly in any windows control that uses icons, like the NotifyIcon control.

The Conversation

Follow the reactions below and share your own thoughts.

  • http://kaminda.blogspot.com/ Kaminda

    G8 Thanks, It saved me very valuble time. Cheers

  • Hossrod

    I ran into one hiccup that took me a while to figure out, but if the icons are in a sub-folder, you need to include the name in the namespace.

    For example, if Filename.ico was in a folder called Icons, it would be reffrenced as “MyNameSpace.Icons.Filename.ico”.

  • Tom

    Icon ico = Properties.Resources.ICONNAME;

    8P

  • keddy

    thanks Tom