|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThere 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 iconThere are a number of ways to add icons to your application for use with controls like 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 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 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! AnswerTo 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 _
The This code snippet is a great help to find what resources you have available to you: Function GetListOfEmbeddedResources() As Array
Return _
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 shotGoogle rocks. CodeProject rocks. Don't know how I managed to code jack before the Internet came along. Enjoy!
|
||||||||||||||||||||||