Search my blog articles

Wednesday, February 10, 2010

Accessing project resource images with a dynamic names.

Let me start off by saying, there are other better ways of accessing this data from the resource file than as listed below. With time and resources available I did this.

Here are my requirements,

1. Need to access images from project resources to bind to controls.

2. The names are dynamic and are DB driven.

So I added all the images I need to the project resource and added all the image names in the DB.

Here is how I was able to access the images from resources by passing in an image name.

I opened the Resources.Designer.cs and added this method.



Now you can access all your images with this line.





Now anywhere in my windows project I can use



to access resource images.


Happy code writing!

1 comment:

Craig Finck said...

This line throws an exception:

name = name.Remove(name.IndexOf('.'), name.Length);

I'd use:

if (string.IsNullOrEmpty(imageName))
return null;

int i = imageName.IndexOf('.');
if (i >= 0)
imageName = imageName.Remove(i);

Thanks for the helpful code.