Dana Yeger
Dana Yeger

Reputation: 711

Change picture box picture

I added picture box to my form and import 2 pictures, from properties under image property i choose the first picture when the application starting and inside my start button event i want to change my picture to the other picture. this is what i have try:

pbIndicator.Image = Image.FromFile(@"..\Resources\indicator_green.png");

but file not found exception error occurs.

Upvotes: 0

Views: 298

Answers (3)

Adriano Repetti
Adriano Repetti

Reputation: 67148

As I wrote in the comment if indicator_green.jpg is an image included as resource via resource file (Resources.resx) then it won't be copied to output directory (it means it's in your project folder because it's used to build executable but it'll be embedded inside your assembly, not deployed standalone).

Resource files will (by default) place resources you add inside Resources folder (and then linked). You can always access them using generated code file for resources:

pbIndicator.Image = Properties.Resources.indicator_green;

You may change namespace Properties and property name according to what you have in your project (by default property name has the same name of the resource and then same name as original file).

Of course you're not forced to embed your resources in your assembly. If you want to deploy them as standalone files just right click Resources folder and add an existing file. In the properties window for that file select Copy always for Copy to output directory and et voila, you'll be able to read it with:

pbIndicator.Image = Image.FromFile(@"Resources\indicator_green.png");

Please note that Resources folder won't be a sub-directory of your output directory (do not forget that source files are not part of installation).

Anyway I suggest you do not build path like that, little bit better would be to do not rely on current folder:

pbIndicator.Image = Image.FromFile(
    Path.Combine(Application.StartupFolder, @"Resources\indicator_green.png");

You're not limited to Resources folder, you can do that with any folder (and with any name).

Upvotes: 0

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

You should be able to do something like this:

pbIndicator.Image = Resources.indicator_green;

Upvotes: 2

Jeroen van Langen
Jeroen van Langen

Reputation: 22083

Be sure that in the property window if the Build Action is on Content, and Copy to Output Directory is on Copy if newer.

If you want it to be content. Else use the answer Shadow Wizard gave.

Upvotes: 1

Related Questions