泛型的读取每次都是最新的一个 为什么

有时想起 2014-10-28 01:11:04

private List<Image<Bgr, Byte>> pic = new List<Image<Bgr, Byte>>();

private void btnSavePic_Click(object sender, EventArgs e)
{
int i = count;

Image<Bgr, Byte> frame = new Image<Bgr, byte>(400, 200);
frame = _capture.RetrieveBgrFrame();
pic.Add(frame);

savedImageBox.Image = frame; //保存下当前的图像
Bitmap bit = savedImageBox.Image.Bitmap;
Image image = MakeThumbnail(bit, 80, 80);
this.imageList1.Images.Add(image);
this.imagelistView.Items.Add(count++ + "", i - 1);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.imagelistView.SelectedItems.Count > 0)
{
savedImageBox.Image = pic[(this.imagelistView.SelectedItems[0].Index)];
}
}

随便点listview的任一项,savedImageBox的图片都只显示了最新存下来的那个pic。我知道肯定是泛型的add方法那有问题。但对frame我不也已经是new了嘛 为什么
...全文
127 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
有时想起 2014-10-29
  • 打赏
  • 举报
回复
引用 6 楼 diaodiaop 的回复:
你这是网页吧.... 如果是网页的话 就能说通了 因为每次你的pic都是新的...没有状态... 所以不如装在viewstate里面

public List<Image<Bgr, Byte>> pic
    {
        get
        {
            object o = ViewState["pic"];
            return o == null ?new List<Image<Bgr, Byte>>():o as List<Image<Bgr, Byte>>();
        }
        set
        {
            ViewState["pic"] = value;
        }
    }
不是网页
有时想起 2014-10-28
  • 打赏
  • 举报
回复
引用 4 楼 Z65443344 的回复:
frame = _capture.RetrieveBgrFrame(); 还有,你确定每次点下按钮,这个frame都是不同的图像吗
确定 已经解决了。我不用pic.Add(frame)。。用下面的bit,通过Image<Bgr, Byte> frame2 = new Image<Bgr, byte>(bit); add这个frame2就可以了。
有时想起 2014-10-28
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
你只是对 this.imageList1.Images 执行了 Add,跟 pic 无关。
是跟pic无关 imaglist只是用来存图片给listview显示的。pic只是每次存的时候存进范型类罢了。其实后来的效果我是做点击了listview的各item后 直接用pic对应的图去显示。
by_封爱 版主 2014-10-28
  • 打赏
  • 举报
回复
你这是网页吧.... 如果是网页的话 就能说通了 因为每次你的pic都是新的...没有状态... 所以不如装在viewstate里面

public List<Image<Bgr, Byte>> pic
    {
        get
        {
            object o = ViewState["pic"];
            return o == null ?new List<Image<Bgr, Byte>>():o as List<Image<Bgr, Byte>>();
        }
        set
        {
            ViewState["pic"] = value;
        }
    }
  • 打赏
  • 举报
回复
你只是对 this.imageList1.Images 执行了 Add,跟 pic 无关。
於黾 2014-10-28
  • 打赏
  • 举报
回复
frame = _capture.RetrieveBgrFrame(); 还有,你确定每次点下按钮,这个frame都是不同的图像吗
於黾 2014-10-28
  • 打赏
  • 举报
回复
this.imagelistView.Items.Add(count++ + "", i - 1); 最好别这么用,先执行++,后执行Add,不要玩C语言2级考试考的那一套
bdmh 2014-10-28
  • 打赏
  • 举报
回复
先检查SelectedItems[0].Index的值是否每次都一样
exception92 2014-10-28
  • 打赏
  • 举报
回复
别用count++,使用count = count +1 ; 试试

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧