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.

FC Jesse
  • 3
  • 4
  • check this question: http://stackoverflow.com/questions/7873453/getting-icon-from-resourcestream – w.b Jan 01 '14 at 17:34

4 Answers4

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.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • 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. – FC Jesse Jan 02 '14 at 11:31
  • 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. – FC Jesse Jan 02 '14 at 16:00
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)

Alberto
  • 15,626
  • 9
  • 43
  • 56
  • I'm using WinForms so I did this: imgIcon.ImageLocation = "pack://application:,,,/project1;component/Properties.Resources._" + iconVar; It doesn't seem to work. – FC Jesse Jan 02 '14 at 11:49
  • I only used the trick with the "pack://". I didn't copy his code. – FC Jesse Jan 02 '14 at 16:02
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");
Community
  • 1
  • 1
Sam
  • 1,358
  • 15
  • 24
  • 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! – FC Jesse Jan 02 '14 at 16:03
  • @FCJesse Have a look at the update, I added a way of loading a resource based on a string. – Sam Jan 03 '14 at 16:01
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!

FC Jesse
  • 3
  • 4