0

I want to access my resources this way:

string iconVar = "20"; imgIcon.Image = "Properties.Resources." + iconVar; 

This is not possible because I give a string and not an image.

So the question is, how can I access the resources without giving the name of the resource in the code?

I know normally you should use

imgIcon.Image = Properties.Resources.image1; 

The thing is, I don't know yet if it's gonna be image1 or image 2 that I'm gonna use.

I tried this:

imgIcon.ImageLocation = "pack://application:,,,/project1;component/Properties.Resources._" + iconVar; imgIcon.Refresh(); 

But that doesn't seem to work.

1

4 Answers 4

0

You say you do not know yet what image you will use, why is this? And is there a reason you want to determine what image you want based on a string?

If you plan on setting the images based on certain conditions, you could try this instead of working with a string:

Image image1 = Properties.Resources.image1; Image image2 = Properties.Resources.image2; imgIcon.Image = someBool ? image1 : image2; 

If you have not decided yet what image you want for the final version and you want to easily change it everywhere in your code simultaneously, define the image as a global variable and use the variable.

Edit:

To load an image based on the name of said image, you could try the following (not tested myself, but taken from this answer):

imgIcon.Image = (Image)Resources.ResourceManager.GetObject("20"); 
Sign up to request clarification or add additional context in comments.

2 Comments

In my resources I have icon 1 till icon 500. Via a web request (json) I get the number of which icon I'm gonna use. Then I want to display that icon in a picturebox. I could use 500 cases. But that's not really clean. Hope I explained enough. Thanks for the answer though!
@FCJesse Have a look at the update, I added a way of loading a resource based on a string.
0

I'll make a few assumptions:

  • you're using WinForms
  • you've already added the image to your project resources as an "image"
  • the imgIcon control is a PictureBox

If that's all true, then you should be able to assign it directly.

imgIcon.Image = Properties.Resources.YourImageName 

I'm not sure what you mean by "without giving the name of the resource". If you want to specify an image, you'll have to give its name.

2 Comments

Sorry for not enough information. That's basically it. But the thing is, I don't know if I want to use YourImageName1 or YourImageName2.
I know. What I mean is, I have a string, the string is the name of the resource. The string is called iconVar. iconVar can either be image1, image2.
0

If we are talking about wpf, I think you need something like this:

BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.UriSource = new Uri("pack://application:,,,/YourApplicationName;component/Properties.Resources."+iconVar, UriKind.RelativeOrAbsolute); bi.EndInit(); imgIcon.Image.Source = bi; 

make sure to replace YourApplicationName with your actual application name (the name of the assembly/exe)

2 Comments

I'm using WinForms so I did this: imgIcon.ImageLocation = "pack://application:,,,/project1;component/Properties.Resources._" + iconVar; It doesn't seem to work.
I only used the trick with the "pack://". I didn't copy his code.
0

I fixed it. This is the code I used:

Assembly _assembly; Stream _imageStream; _assembly = Assembly.GetExecutingAssembly(); _imageStream = _assembly.GetManifestResourceStream("project.Resources._" + iconVar + ".png"); imgIcon.Image = new Bitmap(_imageStream); 

Thanks you for your responses!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.