如何将多个图像添加到列表框并在选中时将其显示在图片框中 [英] how to add multiple images to listbox and display them in picturebox when selected

查看:94
本文介绍了如何将多个图像添加到列表框并在选中时将其显示在图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用fileDialog选择多个图像,然后将图像添加到列表框中。当在列表框中选择一个项目时,我想要将图像显示在图片框中。这是我的代码



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

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

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

         picImageDisplay.Image = img;

推荐答案

试试这个:



Try this:

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



hth Stoffy


非常感谢 stoffy 我尝试了你的代码片段,它解决了我一直在与之斗争的问题
Thank you very much stoffy I tried your code snippet and it solved the problem i have been battling with for days


这篇关于如何将多个图像添加到列表框并在选中时将其显示在图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆