将openfiledilog中的多个图像添加到图像列表中,并在列表视图中显示它们

时间:2014-06-04 04:40:36

标签: c#

你好朋友我是新来的c#我在一个项目中,我想要帮助

我想将openfiledilog中的多个图像添加到图像列表中,并在列表视图中显示它们。

它添加图像但显示相同的图片  这里是我的代码..请帮帮我

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

1 个答案:

答案 0 :(得分:2)

您需要遍历FileNames数组,而不是使用FileName属性。

    int b = 0;
    private void Form1_Load(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("1");
            listViewItem.ImageKey = a;
            b++;
        }
    }

int b = 0;
private void Form1_Load(object sender, EventArgs e)
{
    var ofd = new OpenFileDialog();
    ofd.Multiselect = true;
    ofd.ShowDialog();
    foreach (string fileName in ofd.FileNames)
    {
        Image img = Image.FromFile(fileName);
        string a = b.ToString();
        imageList1.Images.Add(a, img);
        var listViewItem = listView1.Items.Add("1");
        listViewItem.ImageKey = a;
        b++;
    }
}