views:

3344

answers:

4

I need to make an array of images using references to the resources folder.

The following is my code which does not work:

Dim imgPictures(8) As Image
imgPictures(0) = Image.FromFile(My.Resources.cat_1.ToString)

How do I reference the images sitting in the resouces folder for this task?

Cheers

A: 

You can use the My.Resource.GetString("ResourceKey{0}") method for it.

Kirtan
I don't understand the implementation for this in an Image array
burntsugar
A: 

The easiest way to reference a Resource in code is to add it to the project resources.

  • Right click on the project and choose Properties
  • Select the Resources Tab
  • Change the Combo Box to Images
  • Select the "Add Existing" Image from the Add Resource Combo Button
  • Choose your Image

You can then reference the image directly in code using the following

Dim img = My.Resources.NameOfTheImage
JaredPar
I don't understand the implementation for this in an Image array
burntsugar
A: 

Dim imgPictures(8) As Image imgPictures(0) = My.Resources.ResourceManager.GetString("myResourceName")

...

' For asign a resource to a image control based on your array of strings use something like this: Me.picture1.Image = My.Resources.ResourceManager.GetObject(arr(i))

A: 

I think JaredPar was on the right track, but a little more info is needed.

In essence, it kind of depends on how the resources are stored. (If you are looking to get the path of a resource, you will most likely have to look into reflection.)


If your images are embedded or content you can reference them directly:

-(Right Click Image in Visual Studio > Properties > Build Action = "Embedded Resource"

-(Right Click Image in Visual Studio > Properties > Build Action = "Content"; also ensure Copy To Output Directory = "Copy Always")

Dim imgPictures(8) As Image
imgPictures(0) = My.Resources.NameOfImage1
imgPictures(1) = My.Resources.NameOfImage2
...

If your images are just in a folder:

Dim imgPictures(8) As Image
imgPictures(0) = Bitmap.FromFile( <filename1> )
imgPictures(1) = Bitmap.FromFile( <filename2> )
...

Scott

codefoo