列出<img/>异常“参数无效”。

时间:2012-05-18 03:18:54

标签: c# image list

我对List<Image>

有疑问
    List<Image> _Images = new List<Image>();
    int currIndex = 0;

    private void btnAdd_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog dialog = new OpenFileDialog())
        {
            dialog.Filter = "All Images|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
            if (dialog.ShowDialog() == DialogResult.Cancel) 
                return;
            _Images.Add(Image.FromFile(dialog.FileName));
            currIndex = _Images.Count - 1;
            picBox.Image = _Images[currIndex];
        }
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (currIndex + 1 >= _Images.Count)
            return;
        picBox.Image = _Images[++currIndex];
    }

    private void btnBack_Click(object sender, EventArgs e)
    {
        if (currIndex - 1 < 0)
            return;
        picBox.Image = _Images[--currIndex]; 
    }

我将两张图片添加到该列表后,当我按下后退按钮btnBack_Click时出现此异常:Parameter is not valid.
当我第一次添加图像picBox.Image = _Images[currIndex];时,为什么它会起作用呢?当我稍后尝试从索引中获取图像时,它会给我这个例外?

注意:我没有使用ImageList,因为据我所知,它有ImageSize,它对所有图像都是常量。

那我怎么能让它发挥作用呢?

更新
当我将List<Image>更改为List<Stream>

时,它现在有效
picBox.Image = Image.FromStream(_Images[--currIndex]); 

1 个答案:

答案 0 :(得分:1)

将您的List<Image>更改为List<Stream>

picBox.Image = Image.FromStream(_Images[--currIndex]);

然后它会起作用。