ArcanaForce0
ArcanaForce0

Reputation: 55

Import Picture from file to Picture Box

In my project, I have a list box. When I click an item on the listbox, I want the PNG image from a file (stored in 1Global Varible, GV.dir1) into the Picture Box named picBox... this is what I have...

picBox.Image = Image.FromFile(GV.dir + 
                               lstFull.SelectedIndex.ToString() + ".png");

GV.dir is equal to -> @"C:\Files"

Upvotes: 2

Views: 7385

Answers (2)

Stewbob
Stewbob

Reputation: 16899

You're missing a \ after "C:Files", and are your png's really named 0,1,2,3...etc. Using the .SelectedIndex property will just return the index number (as a string with the .ToString). I think you may want to use SelectedItem.ToString instead.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564821

You probably need to change this to:

var imageFile = System.IO.Path.Combine(GV.dir, lstFull.SelectedItem.ToString() + ".png");
picBox.Image = Image.FromFile(imageFile);

Note the use of Path.Combine and SelectedItem. The first takes care of missing \ characters in your path. The second will change your text from a number (index) to the text of the item.

Upvotes: 0

Related Questions