2012-03-01 110 views
4

處理ImageList對象的適當方法是什麼?處理ImageList

假設我和private ImageList imageList成員有一些類。現在,在某些時刻,我執行下面的代碼:

// 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方法實現,其執行方式如下:

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

     disposed = true; 
    } 
} 

我敢肯定有這段代碼存在一些潛在的問題,所以請你幫我把它改正。

+0

相關:[ImageList中:處置原始圖像從列表中刪除它(http://stackoverflow.com/questions/17639237/imagelist-disposing-the-original-image-removes-it-from-列表)。在[ImageList'中的'originals'字段的參考源中的註釋](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ImageList.cs,75)也值得一讀。 – jrh 2016-10-11 18:34:58

回答

4

是的,它做了一個副本。請注意下面的CreateBitMap調用。因此,爲了儘可能降低您的資源使用量,您應該取消註釋處理行。

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配置時,它配置其所有圖像的副本。所以再一次,是的,在表單關閉時處理它是正確的,除了取消註釋其他的處理線。

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

ImageList不擁有對原始圖像的引用。當您添加圖像時,ImageList會複製它。 您可以自由處置原件,因爲您發現方便。
但是,您應該在Dispose()中調用imageList.Images.Clear();