ImageList Removal

How can I place an ImageList ?

Suppose I have a class with a member private ImageList imageList . Now, at some point, I am executing 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; 

In the same class, I have an implementation of the Dispose method, which runs as follows:

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

I am sure there are some potential problems with this code, so could you please help me get it right.

+4
source share
2 answers

Yes, he makes a copy. Pay attention to the CreateBitMap call below. Therefore, to make your use of resources as low as possible, you must uncomment your position.

  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; } 

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

 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); } 
+4
source

ImageList does not have a link to the original image. When you add an image, ImageList copies it. You can freely dispose of the original at your convenience. However, you should call imageList.Images.Clear(); into your Dispose ().

+1
source

Source: https://habr.com/ru/post/1399163/


All Articles