1
PictureBox CharacterIcon = new PictureBox();    
CharacterIcon.Image = Properties.Resources.Moeyy;

I want to make the code dynamic and do something like this:

string fileName = "Moeyy";
PictureBox CharacterIcon = new PictureBox();
CharacterIcon.Image = Properties.Resources.fileName;

But I cannot do that, is there a way around it? I NEED to use the images from Resources because I can't guarantee (that if I load them from a directory) the user will have them in that same directory! I imported the Images into my Resources.resx, so Properties.Resources.Moeyy is an Image that is within the project.

3
  • 1
    What images? Are you deploying images with your project? Are you talking about resx resources? Do you have images or filenames in resx? What are you trying to do exactly? Commented Oct 18, 2015 at 13:40
  • I'm talking about resx resources, I imported an image into it and the Image's name is "Moeyy", I'm assigning that Image to a PictureBox.Image object. I have Images in resx, not filenames. Commented Oct 18, 2015 at 13:55
  • @guntbert Oh, sorry! I was editing and must have overlooked it! But that was all the explanation that was needed since I got my answer =3 Commented Oct 18, 2015 at 17:59

1 Answer 1

2

What you need to do is: use ResourceManager.GetObject(filename) it returns the object at a given key.

You will have to explicitly cast it: (Image)ResourceManager.GetObject(key) and you should have it in a try catch statement so that if there is an error your program doesn't crash.

Here is my source for this: https://msdn.microsoft.com/en-us/library/963f81yd.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.