单击listView中的图像并使其显示在pictureBox中

时间:2014-04-05 14:21:39

标签: c# image listview picturebox imagelist

我已成功导入并在列表中显示了视图从文件夹/目录中查看所有图像,现在我希望能够点击其中一个并在其上方的大图片框中显示该图像,名为" mainPictureBox& #34 ;.我认为我接近这个结果,但我所管理的是让100x100pixel图像出现在我从listview点击的mainPictureBox上,而不是文件夹中的高质量.PNG或.JPG。我猜我需要使用ImageKey或IndexKey,或者以某种方式将文件夹中图像的名称与点击的listView项目的索引相关联。我附加了GUI的图像和用于imageList和listView的代码片段(如果有帮助的话)。

http://i.imgur.com/GkF1hNd.jpg< --- GUI截图

DirectoryInfo dir = new DirectoryInfo(@"C:\Users\UserName\Desktop\PhotoEditorProject\bin\Debug\Images");
        foreach (FileInfo file in dir.GetFiles())
        {
            try
            {
                this.imageList1.Images.Add(Image.FromFile(file.FullName));
            }
            catch
            {
                Console.WriteLine("This is not an image file");
            }
        }

        for (int i = 0; i < this.imageList1.Images.Count; i++)
        {
            ListViewItem item = new ListViewItem();
            item.ImageIndex = i;
            this.listView1.Items.Add(item);
        }

1 个答案:

答案 0 :(得分:0)

亲爱的朋友试试这个..

 int b = 0;
    public void button1_Click_1(object sender, EventArgs e)
    {
        var ofd = new OpenFileDialog();
        ofd.Multiselect = true;
        ofd.ShowDialog();
        for (int z = 0; z < ofd.FileNames.Length; z++)
        {
            Image img = Image.FromFile(ofd.FileNames[z]);
            string a = b.ToString();
            imageList1.Images.Add(a, img);
            var listViewItem = listView1.Items.Add(ofd.FileName );
            listViewItem.ImageKey = a;
            b++;
        }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string s= listView1.SelectedItems.ToString();
       Bitmap bm= new Bitmap (@"" +s);
       pictureBox1.Image = bm;
    }