4

我有问题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_ClickParameter is not valid.
为什么当我第一次添加图像时它会起作用,picBox.Image = _Images[currIndex];然后当我稍后尝试从索引中获取图像时,它给了我这个异常?

注意:我没有使用 ImageList,因为据我所知,它的 ImageSize 对于所有图像都是恒定的。

那我怎么让它工作呢?

更新:
它现在工作了,当我List<Image>改为List<Stream>

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

1 回答 1

1

改变你List<Image>List<Stream>

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

然后它将起作用。

于 2012-05-25T11:50:04.173 回答