Click here to Skip to main content
14,237,394 members

Embedding Icons In Your VB.NET Application

Rate this:
4.81 (65 votes)
Please Sign up or sign in to vote.
4.81 (65 votes)
13 Jun 2003
How to embed and use icons in your application, so you don't have to keep them as separate files.


There are lots of ways to add graphics and other non-code (a.k.a. resource) files to your projects. I went looking for a way to embed some icons in my application without having them as separate files--I wanted them to be stored in the .exe itself. I learned how to do what I was looking for, and I picked up a couple other tricks along the way.

50 ways to leave your icon

There are a number of ways to add icons to your application for use with controls like NotifyIcon (the control that puts an icon in your system tray). The NotifyIcon lets you specify a default icon, for example. You just tell it the file name of your icon, and it adds it to your project, hidden away with the control. But what happens if you want to change the icon in your NotifyIcon during runtime? Where do you put those icons?

Well, I did some research on this site and Google, and I found a few ways of doing it. The first is to simply store the icons as separate files associated with your project, and then use code like this to refer to get the icon for use in your application:

Dim objIcon As New Icon("c:\myicon.ico")

The trouble with this technique is that you have to be careful to keep the icon in the proper path associated with your application. It's a little work, and I figured there must be a more elegant solution. I had noticed that many apps keep their many icons in the app itself, so my search continued with that in mind.

What I found next was a way to embed the icons in an application through the use of an ImageList control, which is basically a collection of bitmaps. You add the graphics via the ImageList control at design-time and you can refer to each image by index number at run-time. But, since these images are stored as bitmaps, you must change the image from a Bitmap to an Icon before it may be used as an icon with your NotifyIcon control:

Dim objBitmap As Bitmap
Dim objIcon As Icon
Dim intIndexOfImageIWant As Int16 = 1
objBitmap = New Bitmap(objImageList.Images(intIndexOfImageIWant))
objIcon = System.Drawing.Icon.FromHandle(objBitmap.GetHicon)

Well, that seemed like a pretty good solution, until I realized that an ImageList only stores images as bitmaps at a single resolution. So if I have all these great icons in various sizes that I want to use throughout my application, maybe using their different sizes (since icons have multiple sizes stored within their single file), the ImageList wasn't quite going to cut it.

Next, I discovered talk of the code-Jedi's way of doing things: resource files. These are files that have icons, graphics, and text all embedded in them and ready for your application to use. .NET even stores the info as XML! Cool. But, to create the resources files, you have to get a utility to compile the resource file, and then use some cool code to extract and use what's inside. This way of doing things is useful when you want to localize an application--creating different versions for different languages becomes as easy as swapping out resource files. But, I didn't feel my apps called for that much extra effort, each time I had a few icons to embed. Besides, the goal wasn't to create an extra file with my icons in it, I wanted it in my main exe!


To embed your icons in your application, drag them into your .NET project. Edit the properties for each one. See the "Build Action" property that's set to "Content"? Change it to "Embedded Resource." That will make it compile into your exe or DLL. That's it! You've just embedded an icon or two into your application. do you get it back out?

With a little love, and a little code:

Function GetEmbeddedIcon(ByVal strName As String) As Icon
    Return New _<BR>Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(_<BR>     strName))
End Function

The strName parameter is of the format <appNameSpace>.<filenamewithoutextension>.<filenameextension> so, something like MyApplication.MyLogo.ico. It is case sensitive, so watch for that one. If you don't use the name of a resource it can find, you will get an error saying something about a Null.

This code snippet is a great help to find what resources you have available to you:

Function GetListOfEmbeddedResources() As Array
    Return _<BR>     System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceNames
End Function

It caught a couple mis-capitalizations for me that otherwise would have left me with tufts of torn out hair, as I spent fruitless hours trying to figure out why it couldn't find the resource I had so clearly embedded.

Parting shot

Google rocks. CodeProject rocks. Don't know how I managed to code jack before the Internet came along. Enjoy!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Scott Bonds
Web Developer
United States United States
Scott is a producer with Electronic Arts in Redwood City and he's an amateur coder.

Comments and Discussions

AnswerRe: How do you use an embedded icon library? Pin
Scott Bonds7-Oct-03 10:12
memberScott Bonds7-Oct-03 10:12 
GeneralRe: How do you use an embedded icon library? Pin
aresium17-Oct-03 4:34
memberaresium17-Oct-03 4:34 
GeneralRe: How do you use an embedded icon library? Pin
Scott Bonds19-Oct-03 8:39
memberScott Bonds19-Oct-03 8:39 
GeneralRe: How do you use an embedded icon library? Pin
Andrew Way21-Nov-03 18:43
memberAndrew Way21-Nov-03 18:43 
GeneralEmbedding HTML and accessing it Pin
WGenc4-Aug-03 9:43
memberWGenc4-Aug-03 9:43 
GeneralRe: Embedding HTML and accessing it Pin
Scott Bonds5-Aug-03 10:58
memberScott Bonds5-Aug-03 10:58 
GeneralRe: Embedding HTML and accessing it Pin
WGenc5-Aug-03 11:59
memberWGenc5-Aug-03 11:59 
GeneralRe: Embedding HTML and accessing it Pin
Scott Bonds5-Aug-03 14:02
memberScott Bonds5-Aug-03 14:02 
GeneralRe: Embedding HTML and accessing it Pin
WGenc6-Aug-03 5:25
memberWGenc6-Aug-03 5:25 
GeneralRe: Embedding HTML and accessing it Pin
Mike Gore13-Nov-03 3:21
memberMike Gore13-Nov-03 3:21 
GeneralRe: Embedding HTML and accessing it Pin
Chris Weed30-Nov-03 12:52
memberChris Weed30-Nov-03 12:52 
GeneralRe: Embedding HTML and accessing it Pin
Chris Weed30-Nov-03 12:56
memberChris Weed30-Nov-03 12:56 
GeneralThx Pin
Querulant18-Jun-03 6:58
memberQuerulant18-Jun-03 6:58 
GeneralRe: Thx Pin
Scott Bonds18-Jun-03 12:26
memberScott Bonds18-Jun-03 12:26 
GeneralJust what I needed Pin
Brian Stevens17-Jun-03 15:18
memberBrian Stevens17-Jun-03 15:18 
GeneralRe: Just what I needed Pin
Scott Bonds17-Jun-03 15:41
memberScott Bonds17-Jun-03 15:41 
QuestionHow to embed AVI Files Pin
Raj7816-Jun-03 22:01
memberRaj7816-Jun-03 22:01 
AnswerRe: How to embed AVI Files Pin
Scott Bonds16-Jun-03 22:48
memberScott Bonds16-Jun-03 22:48 
GeneralYEAH bb Pin
Jan Vercauteren15-Jun-03 9:38
memberJan Vercauteren15-Jun-03 9:38 
GeneralRe: YEAH bb Pin
Scott Bonds16-Jun-03 23:03
memberScott Bonds16-Jun-03 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 13 Jun 2003


113 bookmarked