Call Image.Dispose when clearing an Image List?

  • Thread starter Thread starter Trev Hunter
  • Start date Start date
T

Trev Hunter

Hi,

I have a listview which is set to view items in a small icon view. It's set
up to get the images from an image list.

When I populate the listview, I clear the image list and add new images for
each item. This seems to work fine, but what I was wondering was if I should
call the Dispose method on the images when clearing the imagelist before
repopulating the listview?


Currently, I'm doing the following:

----------------------

' Clear the lists
lstItems.Items.Clear
ilstItemImages.Images.Clear

' Before calling clear, should I get a
' reference to the images and call
' Dispose() after calling clear?


' Add a new item
' Add a new icon for the item
.....
 
Hi Trev,
Based on my understanding, you have some items in the list (smallicon view). Then you want to change items and icons.
From your description, do you mean that you want to apply Dispose on the Images (ImageList.ImageCollection)? Or you want to clear the images by yourself
instead of letting CLR's GC handle it.

In the first case, it is not supported.

In the second case, if it is not where you must clean up the resource or not a place where you know it is safe to do this for performance improving, I suggest you
using clear to remove all the images and masks from the ImageList and then add new icons for items .


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Hi, thanks for the tips. Let me explain a bit further what I am doing:

1) I populate the listview with a list of files
2) When I add an item to the listview, I use the API call SHGetFileInfo to
get the icon for the file type
3) I add the icon to the image list

From time to time, the list is changed (cleared an repopulated). Since I
added the individual images at runtime instead of from a resource, do I need
to call dispose() on the individual images before I call
ImageList.Images.Clear(), or will the ImageList handle disposing of the
images automatically?

Thanks again for your help,

Trev.

Rhett Gong said:
Hi Trev,
Based on my understanding, you have some items in the list (smallicon
view). Then you want to change items and icons.
From your description, do you mean that you want to apply Dispose on the
Images (ImageList.ImageCollection)? Or you want to clear the images by
yourself
instead of letting CLR's GC handle it.

In the first case, it is not supported.

In the second case, if it is not where you must clean up the resource or
not a place where you know it is safe to do this for performance improving,
I suggest you
using clear to remove all the images and masks from the ImageList and then add new icons for items .


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Hi Trev,
I think you don't need to apply dispose on the images. Imagelist will
handle disposing of images automatically.

If you want to clear them immediately, I suggest you using
GC.Collect() after you call the Clear().

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no
rights.
Please reply to newsgroups only. Thanks.
 
Back
Top