This will be just a quick post about using image resources in windows applications. I prefer to add resources as Resources (capital R) to my projects so that they are embedded in the exe. This works well for most of the smaller apps I create but it probably wouldn’t be the best idea if you were using a TON of images as the exe could start to become very large. The process is really easy.
In Visual Studio, you can right click on the application and choose “Properties”. This should open a multi tabbed pane. Select “Resources” and change the drop down type from String to Images. This method should work for the other types you see in the drop down as well (Audio, Icon, Files, etc).
From here I’ve found that I need to drag and drop the image files into the Resource pane, as clicking Add Resource tries to create a new one. If you do click it, or if you just want to get rid of a Resource you’ve added, simply select the Resource and click “Remove Resource.” You’ll also need to select the file in the Resource folder and delete it there as well.
I had some issues with this where after I deleted some resources it kept giving me errors when I tried to compile saying that I was missing those files. I couldn’t find a reference to them anywhere. It was really annoying because it was a dialog box that would open again after you clicked “OK”. It would reopen itself something like 10 times. I fixed this by adding a Resource with that name again and following the above method for removing them. I think I may have simply deleted them the first time without doing “Remove Resource” first.
Accessing your new resource programatically is really simple - as they are now global resources they can be accessed like this:
pictureBoxExample.Image = Properties.Resources.MyImageResourceName;
Nice and easy. It even returns the correct type so there is no need for casting. There are other ways to access the resources you have added but I’m not going to cover that here.
If you like this blog please take a second and subscribe to my rss feed
Tags: c#, Code, resources, visual studio
Comments: No comments, be the first to comment
All the fields that are marked with REQ must be filled
Leave a reply