日期:2014-05-18  浏览次数:21189 次

vs2005 中ImageList中InvalidArgument=“1”的值对于“index”无效
private void Form1_Load(object sender, EventArgs e)
  {
  string path = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
  path += @"\01.jpg";
  string path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
  path2 += @"\02.jpg";
  Image ming = Image.FromFile(path,true);
  imageList1.Images.Add(ming);
  Image ming2 = Image.FromFile(path2, true);
  imageList1.Images.Add(ming2);
  imageList1.ImageSize = new Size(200,165);
  pictureBox1.Width = 200;
  pictureBox1.Height = 165;
   

  }

  private void button1_Click(object sender, EventArgs e)
  {
  int a = 0;
  if (a == (imageList1.Images.Count - 1))
  {
  a = 0;
  }
  else if(a<imageList1.Images.Count)
  {
  a = a + 1;
  }

  this.pictureBox1.Image=this.imageList1.Images[a];
  }

  private void button2_Click(object sender, EventArgs e)
  {
  pictureBox1.Image = imageList1.Images[1];
  }
  }



这是代码那里出错了啊,请大家帮忙

------解决方案--------------------
ImageList没有2张图,所以不能用1,如果有1张图,用imageList1.Images[0];

------解决方案--------------------
探讨
引用:
看这代码是看不出问题,你在 private void Form1_Load(object sender, EventArgs e) 的最后,private void button1_Click(object sender, EventArgs e) 最前,private void button2_Click(object sender, EventArgs e)
最前,……

------解决方案--------------------
探讨
Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
这句话是干嘛啊,好像是不是使用错误了

------解决方案--------------------
C# code

我曾经也使用过ImageList,用下标取的时候,也是取不出来,也不知道什么原因;不过有解决办法:
Image ming2 = Image.FromFile(path2, true);
  imageList1.Images.Add(ming2);
你添加的时候这样子添加:
 imageList1.Images.Add("ming2",ming2);
给它添加一个key进去
取的时候通过key来取
例如
imageList1["ming2"]这样子就可以取出来了。
很奇怪,我怀疑是.net的bug