Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been trying to select multiple images using the fileDialog and then add the images to a listbox. When an item is selected in the listbox, I want to image to be displayed in a picturebox. Here is my code

C#<br
private void cmdInputDirectory_Click(object sender, EventArgs e)
       {

           //open a directory/file dialog to copy the image file
           //OpenFileDialog open = new OpenFileDialog();

           //image filters
           inputDialog.Filter = "Picture Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|ALL FILES(*.*)|*.*";

           //allow the user to select multiple image files
           this.inputDialog.Multiselect = true;
           this.inputDialog.Title = "Select Image File(s)";

           //add the selected images to the listbox "lstImages" on the right panel
           DialogResult dr = this.inputDialog.ShowDialog();
               if(dr == System.Windows.Forms.DialogResult.OK)
               {

                   //Read the files and add them to the listbox "lstImages"
                         foreach(string file in inputDialog.FileNames)
                         {
                             lstImages.Items.Add(Path.GetFileName(file));
                         }



                       //image file path
                       txtInputDirectory.Text = inputDialog.FileName;


               }


And here is the code to select item from the listbox

C#
// string selectedItem = lstImages.Items[lstImages.SelectedIndex].ToString() ;
         img = new Bitmap(selectedItem);

         picImageDisplay.Image = img;
Posted

Try this:

C#
public partial class Form1 : Form
{
    string parentDir = "";
    public Form1()
    {
        InitializeComponent();
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string filename = System.IO.Path.Combine(parentDir, listBox1.SelectedItem.ToString());
        pictureBox1.Image = Image.FromFile(filename);
    }

    private void selectFilesButton_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog dlg = new OpenFileDialog())
        {
            dlg.Filter = "";
            dlg.Title = "";
            dlg.Multiselect = true;

            DialogResult dr = dlg.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                foreach (string file in dlg.FileNames)
                {
                    if (string.IsNullOrEmpty(parentDir))
                        parentDir = System.IO.Path.GetDirectoryName(file);

                    listBox1.Items.Add(file);
                }
            }
        }
    }
}


hth Stoffy
 
Share this answer
 
Thank you very much stoffy I tried your code snippet and it solved the problem i have been battling with for days
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900