0

I have some picture box controls created, and on the project, a resource folder. From this resource folder, the picture box controls have a default image. How can I modify, ON-RUNTIME, the image of a picture box control?

What I've done so far is this:

PictureBox photo1 = new PictureBox();
photo1.Image = Image.FromFile(@"path");

But, the problem I've encountered, is, If I would set up the path of the picture box control, It should be the same for all users that will use my application, on different machines.

How can I do to select the images directly from my resource folder?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
G Andrei
  • 154
  • 8
  • is the image embedded as a resource? – Daniel A. White Jan 03 '22 at 21:12
  • I don't properly understand the question. In the Solution Explorer, I have Resource Folder, and there are the images used in project. And, I want, on RUN TIME, related to the IF statement, to change a image with another... Hope you understand what I want to say.. – G Andrei Jan 03 '22 at 21:26
  • You can have resources in your project, but I think you are using that term for a sub-directory of your executable. It's a bit confusing. – LarsTech Jan 03 '22 at 21:42
  • Yes @PalleDue. I've found the solution on that topic. Thank you so much all of you, especially you =) – G Andrei Jan 03 '22 at 22:02

1 Answers1

0

In Windows Forms, resources (also including images), are typically accessed through the dynamically generated Resources class within your projects Properties namespace via static properties.

We can add resources by putting the files into the Resources folder within our project and by adding them to the Properties\Resources.resx file using the resource file editor integrated in Visual Studio. The resources get compiled into the assembly.

Afterwards, we can access the images, e.g., with <default-namespace>.Properties.Resources.MyImage1 (by default, the name of the property is derived from the file name).

PictureBox photo1 = new PictureBox();
photo1.Image = <default-namespace>.Properties.Resources.MyImage1;
Gerald Mayr
  • 644
  • 2
  • 13