What is the filepath of the images that are in the Resources folder of my C # project in VS?

1

I'm trying to make a function that depending on the model of a car, you choose a certain image for a pictureBox.

I was able to put the image in the pictureBox in the following way: pictureBoxAuto.BackgroundImage = Properties.Resources.imagen.jpg;

But, what I need is that it can be put through a filepath, in order to do something like this: pictureBoxAuto.BackgroundImage = ("Resources / image" + model + ". jpg"); This so that, simply by sending a parameter that is the model, the image to be displayed is changed. For example, I have my image called: "carro2012.jpg" So for that I want to know the direct filepath of Resources, because if I put it as pictureBoxAuto.BackgroundImage = Image.FromFile ("C: / blah blah blah") it will not work on other computers.

    
asked by Didier Valdez 27.08.2016 в 10:02
source

1 answer

1

When the image is there as a resource it does not have a physical path to access it

If you want to be able to define a parameter as string with the name of the image you should use the ResourceManager

Recover resources with the ResourceManager class

then you could do

Assembly myAssembly = this.GetType().Assembly;

ResourceManager myManager = new ResourceManager("Properties.Resources", myAssembly);

string imgResource = string.Format("imagen{0}.jpg", modelo);
pictureBoxAuto.BackgroundImage = (Image)myManager.GetObject(imgResource);

remember to define

using System.Reflection;
using  System.Resources;    
using System.Drawing;                   
    
answered by 27.08.2016 в 17:06