0

I have several images in Resources part of a project. Each image is called img_1.png, img_2.png, img_3.png, etc

I have a pictureBox and depending of the value of an index I would like to display a different picture. If index=1, then display img_1.png, etc

Is there a way to access content of Resources with a variable. Something like the following code (which doesn't work)

myPictureBox.Image = myProject.Properties.Resources.img_ + index.ToString() + ".png";

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • there are multiple ways to do. provide some codes (like how you store the resource, resource type, how to get the resource) and i can help you with this – Jacky Jun 21 '18 at 07:39
  • @Jacky I have completed the question to precise image are png. What part of code would you need ? myPictureBox is Winform and I have added images to the resources using Visual Studio (in Property of the project, Ressources, add ressources) – NicolasNicolas Jun 21 '18 at 07:45
  • there is a [related question](https://stackoverflow.com/q/1192054/1132334), [one of its answers](https://stackoverflow.com/a/1192077/1132334) specifically offers a solution where the name of the resource is variable at runtime. – Cee McSharpface Jun 21 '18 at 07:48
  • Please do not use the `visual-studio` tag unless your question is pertaining to the Visual Studio IDE, rather than code simply developed in it. Are you using a Resources file? – ProgrammingLlama Jun 21 '18 at 07:58
  • If you're using a Resources file, then [this](https://stackoverflow.com/questions/47592576/accessing-an-image-resource-by-name-and-assigning-it-to-a-picturebox) should answer your question. – ProgrammingLlama Jun 21 '18 at 08:00

1 Answers1

2

You can use this:

myPictureBox.Image = myProject.Properties.Resources.ResourceManager.GetObject($"img_{index}");
vasily.sib
  • 3,871
  • 2
  • 23
  • 26