你能告诉我如何通过单击listBox中的文件来在pictureBox中加载图像吗

时间:2018-12-02 21:44:47

标签: c#

上传图片后,图片的文件名将显示在列表框中。我想在每次单击列表框中的项目时将图片加载到图片框中,但是我不知道如何操作。感谢谁想帮助我。 :)

这是代码:

public partial class fileee : UserControl
{
    Image<Bgr, byte> imgInput;
    Image<Gray, byte> CC;
    public fileee()
    {
        InitializeComponent();
    }

    // browse file
    private void browse2_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd1 = new OpenFileDialog();
        ofd1.Multiselect = true;
        ofd1.Filter = "Images|*.jpg;*.jpeg;*.png;";
        if (ofd1.ShowDialog() == DialogResult.OK)
        {
            foreach (string item in ofd1.SafeFileNames)
            {
                //textBox1.Text = ofd1.FileName;
                textBox2.Text = ofd1.SafeFileName;
                listBox1.Items.Add(item);
                imgInput = new Image<Bgr, byte>(ofd1.FileName);
                pictureBox2.Image = imgInput.Bitmap;
            }   
        }
    }

    // selected index in listbox
    private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        OpenFileDialog ofd1 = new OpenFileDialog();
        string curItem = listBox1.SelectedItem.ToString();
        textBox2.Text = curItem;

    }

    private void fileee_Load(object sender, EventArgs e)
    {
       listBox1.SelectionMode = SelectionMode.MultiExtended;
    }
}

1 个答案:

答案 0 :(得分:0)

因此,仅在selectedintexChanged事件类型代码上: picturebox.Image = new Bitmap(Image.FromFile(listbox.SelectedItem.ToString()/*only if your item is a path,if not then enter your path*/), picturebox.Size); 应该是