布置一个ImageList [英] Disposing an ImageList

查看:116
本文介绍了布置一个ImageList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处置ImageList对象的适当方法是什么?

What's the appropriate way to dispose an ImageList object?

假设我有一个带有private ImageList imageList成员的类.现在,我会执行以下代码:

Suppose I have some class with a private ImageList imageList member. Now, at some moment I perform the following code:

// Basically, lazy initialization.
if (imageList == null)
{
    imageList = new ImageList();
    Image[] images = Provider.CreateImages(...);
    foreach (var image in images)
    {
        // Does the 'ImageList' perform implicit copying here
        // or does it aggregate a reference?
        imageList.Images.Add(image); 

        // Do I need to do this?
        //image.Dispose();
    }
}

return imageList;

在同一类中,我有Dispose方法实现,该实现通过以下方式执行:

In the same class I have the Dispose method implementation, which is performed the following way:

public void Dispose()
{
    if (!disposed)
    {
        // Is this enough?
        if (imageList != null)
            imageList.Dispose();

        disposed = true;
    }
}

我确定此代码可能存在一些潜在问题,所以请您帮我纠正一下.

推荐答案

是的,它会进行复制.请注意下面的CreateBitMap调用.因此,为了使资源使用尽可能少,您应该取消注释处置行.

Yes, it makes a copy. Note the CreateBitMap call below. Therefore, to keep your resource use as low as possible, you should uncomment your dispose line.

 private int Add(ImageList.Original original, ImageList.ImageCollection.ImageInfo imageInfo)
  {
    if (original == null || original.image == null)
      throw new ArgumentNullException("value");
    int num = -1;
    if (original.image is Bitmap)
    {
      if (this.owner.originals != null)
        num = this.owner.originals.Add((object) original);
      if (this.owner.HandleCreated)
      {
        bool ownsBitmap = false;
        Bitmap bitmap = this.owner.CreateBitmap(original, out ownsBitmap);
        num = this.owner.AddToHandle(original, bitmap);
        if (ownsBitmap)
          bitmap.Dispose();
      }
    }
    else
    {
      if (!(original.image is Icon))
        throw new ArgumentException(System.Windows.Forms.SR.GetString("ImageListBitmap"));
      if (this.owner.originals != null)
        num = this.owner.originals.Add((object) original);
      if (this.owner.HandleCreated)
        num = this.owner.AddIconToHandle(original, (Icon) original.image);
    }
    if ((original.options & ImageList.OriginalOptions.ImageStrip) != ImageList.OriginalOptions.Default)
    {
      for (int index = 0; index < original.nImages; ++index)
        this.imageInfoCollection.Add((object) new ImageList.ImageCollection.ImageInfo());
    }
    else
    {
      if (imageInfo == null)
        imageInfo = new ImageList.ImageCollection.ImageInfo();
      this.imageInfoCollection.Add((object) imageInfo);
    }
    if (!this.owner.inAddRange)
      this.owner.OnChangeHandle(new EventArgs());
    return num;
  }

当处理ImageList时,将处理其所有图像的副本.同样,是的,除了取消注释其他处置行之外,正确的做法是在表单关闭时处置它.

When ImageList disposes, it disposes its copies of all the images. So again, yes, disposing of it when the form closes as you are is the right thing in addition to uncommenting your other dispose line.

protected override void Dispose(bool disposing)
{
  if (disposing)
  {
    if (this.originals != null)
    {
      foreach (ImageList.Original original in (IEnumerable) this.originals)
      {
        if ((original.options & ImageList.OriginalOptions.OwnsImage) != ImageList.OriginalOptions.Default)
          ((IDisposable) original.image).Dispose();
      }
    }
    this.DestroyHandle();
  }
  base.Dispose(disposing);
}

这篇关于布置一个ImageList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆