Click here to Skip to main content
Click here to Skip to main content

Embedding Icons In Your VB.NET Application

By , 13 Jun 2003
 

Introduction

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!

Answer

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. So...how 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!

License

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
Member
Scott is a producer with Electronic Arts in Redwood City and he's an amateur coder.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionEasiest way yet...memberCode_Addict31 Aug '11 - 5:03 
The easiest way that I have found to set an icon for the NotifyIcon is:
Double click on the My Project item in the Solution Explorer.
A docked page will appear, click on the resources tab.
If you have an existing icon file, simply select the drop down arrow on the Add Resources button; select the existing file option and it will be added.
To use the newly added icon use the code below.
MyNotifyIcon.Icon = My.Resources.IconName
You can use this method if you hand code your apps or use it programmatically to either set or change the icon at runtime.
GeneralUse Extrenal IconmemberFastBurst17 Jun '08 - 6:33 
This may sound silly, bu tI am looking to the opposite of this. I have been searching the web for weeks now. I want to build a VB.net Application and have it use an External Icon file for the exe, so if I want to replace the whatever.ico with a new one it shows up like that on the exe when looking at it.
 
Is this even possible? or are all Icons prebuilt into the compile and can't do it.
GeneralThanksmemberNasenbaaer3 Mar '08 - 11:39 
Just wanna say thank you!!
 
Big Grin | :-D
 
www.goldengel.ch
GeneralAnother way to embed iconsmemberrun6553218 Nov '07 - 4:30 
Hello,
 
I usually store all my contents in .resx files, as they are "editable" directly from inside visual studio 2005. When I have to , f.e., specify an icon for my application I go to the project properties and I set it to one of the existing icons in that resource file.
 
As I don't know how to specify the icon for a form directly from the vs graphical interface OMG | :OMG: , I put this code on the load event for my form:
 
Me.Icon = My.Resources.ResourceName
 
I hope this helps ! Smile | :)
 
Roger Tranchez
MCTS
GeneralRe: Another way to embed iconsmemberBOBLAiBAM!13 Nov '07 - 5:27 
very helpful, thanks Roger Smile | :)
GeneralExtract a byte type file (ie an embeded exe file)memberQckSilverDragon17 May '07 - 17:12 
ive been trying to figure out how to do this and incase im not the only one here is a very simple way to output a byte file.
 
Dim ByteFile() As System.Byte = My.Resources.(name of resource)
FileIO.FileSystem.WriteAllBytes("File path and name", ByteFile, False)
GeneralYou Da MAN!!memberElvis_Pretzelator2 Jan '07 - 6:23 
Excellent!!!! Thank you very much!!Big Grin | :-D
GeneralSo how do you do it in VB.Net? (not visual studio)memberGeorge98765432125 Feb '06 - 3:49 
Any solution that involves dragging an item in to visual studio is a cop out!
 
If you don't know how visual studio does what it does behind the scenes then you shouldn't be authoring an article.
GeneralRe: So how do you do it in VB.Net? (not visual studio)memberchermisha24 Nov '06 - 5:46 
I think it IS a solution, probably not the perfect one and it's great that Scott shared it with everybody. There's no need to be rude.
QuestionHow do you determine a NameSpace if one existsmemberSBendBuckeye5 Jan '06 - 11:29 
You code is very nicely done, simple and concise. We use a NameSpace for any UserControls (eg CompanyName.CustomTools). How do you determine the NameSpace for the Executing Assembly so you could generate your correct name by parsing code? Thanks!
 
Have a great day!
 
j2associates_NO_SPAM_@yahoo.com

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 14 Jun 2003
Article Copyright 2003 by Scott Bonds
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid