0

If I have some resources, eg card1.png, card2.png, etc, I want to them load them into a picBox, but only load the correct image eg, something like

int cardNum = rn.Next(0,cardMax);
picCard.Image = Properties.Resources."card"+cardNum+".png";

Obviously that doesn't work, but how would I do what I am trying to do (load the correct resource after building the resource name into a string)

6
  • 1
    Possible duplicate of How to get Name by string Value from a .NET resource (RESX) file Commented Jun 29, 2016 at 8:22
  • 1
    @AndyKorneyev That's about finding a resource's key by matching its content, not loading by name. Commented Jun 29, 2016 at 8:26
  • Open Resources.Designer.resx and you see exactly how they are looked up by string name. Commented Jun 29, 2016 at 8:27
  • You probably need Assembly.GetManifestResourceStream. Commented Jun 29, 2016 at 8:28
  • Note that when adding something to the resources, the extension is generally stripped off. While it's technically allowed, the dot character is deemed a "bad character" in resource names. Commented Jun 29, 2016 at 8:30

2 Answers 2

2

Instead of the generated properties in the Resources class use the ResourceManager directly:

string resName = $"card{cardNum}.png"; // Check the correct name in the .resx file. By using the wizards the extension is omitted, for example.
picCard.Image = (Image)Properties.Resources.ResourceManager.GetObject(resName);
Sign up to request clarification or add additional context in comments.

1 Comment

10th answer i found, first one that worked for me xD
0

Try using:

var rm = new System.Resources.ResourceManager(((System.Reflection.Assembly)System.Reflection.Assembly.GetExecutingAssembly()).GetName().Name + ".Properties.Resources", ((System.Reflection.Assembly)System.Reflection.Assembly.GetExecutingAssembly()));
Image img = (Bitmap)rm.GetObject("resourcesimagename.jpg");

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.