Importing an image from a file into an image window

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

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

      

GV.dir

equals → @"C:\Files"

+2


source to share


2 answers


You are missing \

after "C:Files"

and your pngs are indeed named 0,1,2,3 ... etc. Using the property .SelectedIndex

will simply return the index number (as a string with .ToString

). I think you can use SelectedItem.ToString

this instead.



+2


source


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 will take care of skipping the \ characters in your path. The second one will change your text to number (index) to element text.

0


source







All Articles