imageList1.Images[0].Tag = "000";
            MessageBox.Show(imageList1.Images[0].Tag.ToString());//出错!!!            注:已经为imagelist1添加了几张图片。
错误信息为:未将对象引用设置到对象的实例。

解决方案 »

  1.   

    ImageList.Images[].Tag 赋值属性都是Null 可能是个bug
      

  2.   


    是的,我新建的一个项目,就这两行代码?然后在imaglist里添加了一张图片。
    运行到第二行时出错。调试里显示tag的值为null
      

  3.   


                //imageList1中有四张图片
                Image img = imageList1.Images[0];
                img.Tag = "111";
                imageList1.Images[0] = img;
                MessageBox.Show(img.Tag.ToString());//弹出对话框
                MessageBox.Show(imageList1.Images[0].Tag.ToString());//报错
    个人觉得问题出现在下图 注意看图片属性中是没有Tag这个属性的 但根据上面的代码 当从imageList1中把图片取出来再给Tag赋值是可以的 说明取出来的图片是有Tag属性的 但放到imageList中就丢失了 网上查出来的结果说可能是imageList的一个bug 希望对LZ有帮助
      

  4.   

    在ImageList中获取某索引处的图片是创建新图,因此设置Tag无效,每次访问该属性得到的都是不同的Image对象,为什么每次都要创建新图而不是缓存图片就不知道了。
    public sealed class ImageList : Component
    {
        private ImageList owner;    public Image this[int index]
        {
            get
            {
                if ((index < 0) || (index >= this.Count))
                {
                    throw new ArgumentOutOfRangeException("index", SR.GetString("InvalidArgument", new object[] { "index", index.ToString(CultureInfo.CurrentCulture) }));
                }
                return this.owner.GetBitmap(index);
            }
            set ......
        }
    ......
    }
    下面是GetBitmap方法的具体代码:
    private Bitmap GetBitmap(int index)
    {
        if ((index < 0) || (index >= this.Images.Count))
        {
            throw new ArgumentOutOfRangeException("index", SR.GetString("InvalidArgument", new object[] { "index", index.ToString(CultureInfo.CurrentCulture) }));
        }
        Bitmap image = null;
        if (this.ColorDepth == ColorDepth.Depth32Bit)
        {
            NativeMethods.IMAGEINFO pImageInfo = new NativeMethods.IMAGEINFO();
            if (SafeNativeMethods.ImageList_GetImageInfo(new HandleRef(this, this.Handle), index, pImageInfo))
            {
                Bitmap bitmap2 = null;
                BitmapData bmpData = null;
                BitmapData targetData = null;
                IntSecurity.ObjectFromWin32Handle.Assert();
                try
                {
                    bitmap2 = Image.FromHbitmap(pImageInfo.hbmImage);
                    bmpData = bitmap2.LockBits(new Rectangle(pImageInfo.rcImage_left, pImageInfo.rcImage_top, pImageInfo.rcImage_right - pImageInfo.rcImage_left, pImageInfo.rcImage_bottom - pImageInfo.rcImage_top), ImageLockMode.ReadOnly, bitmap2.PixelFormat);
                    int stride = bmpData.Stride;
                    int height = this.imageSize.Height;
                    if (BitmapHasAlpha(bmpData))
                    {
                        image = new Bitmap(this.imageSize.Width, this.imageSize.Height, PixelFormat.Format32bppArgb);
                        targetData = image.LockBits(new Rectangle(0, 0, this.imageSize.Width, this.imageSize.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                        this.CopyBitmapData(bmpData, targetData);
                    }
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                    if (bitmap2 != null)
                    {
                        if (bmpData != null)
                        {
                            bitmap2.UnlockBits(bmpData);
                        }
                        bitmap2.Dispose();
                    }
                    if ((image != null) && (targetData != null))
                    {
                        image.UnlockBits(targetData);
                    }
                }
            }
        }
        if (image == null)
        {
            image = new Bitmap(this.imageSize.Width, this.imageSize.Height);
            using (Graphics graphics = Graphics.FromImage(image))
            {
                IntPtr hdc = graphics.GetHdc();
                try
                {
                    SafeNativeMethods.ImageList_DrawEx(new HandleRef(this, this.Handle), index, new HandleRef(graphics, hdc), 0, 0, this.imageSize.Width, this.imageSize.Height, -1, -1, 1);
                }
                finally
                {
                    graphics.ReleaseHdcInternal(hdc);
                }
            }
        }
        image.MakeTransparent(fakeTransparencyColor);
        return image;
    }微软是哪根筋不对我就不知道了,还不如自己创建静态变量列表存放图片呢,那样效率更高。
      

  5.   


    我需要保存两个变量,icon图片和string图片名称。您看这样行吗?
    struct img
    {
      icon ico
      sring name
    }list<img> imglst =new list<>
      

  6.   

    可以,把那个imglst设置为静态的,那样效率会更好,从任何地方都可以访问,不要的时候也可以对图片逐一进行Dispose,释放资源。