ListViewImageList keeps bitmap locked after disposing

Q

Qwert

Hello,

I have a listview with an image list. After dispoing the images and the
image list, the bitmap files remain locked:

REM Create.
objImage = Image.FromFile(strBmp)
If Me.lsvTest.LargeImageList Is Nothing Then
Me.lsvTest.LargeImageList = New ImageList()
End If
Me.lsvTest.LargeImageList.Images.Add(objImage)
objLVI = Me.lsvTest.Items.Add(strBmp)
objLVI.ImageIndex = Me.lsvTest.LargeImageList.Images.Count - 1
objImage = Nothing

REM Delete.
For Each objImage In Me.lsvTest.LargeImageList.Images
objImage.Dispose()
objImage = Nothing
Next
Me.lsvTest.LargeImageList.Images.Clear()
Me.lsvTest.LargeImageList.Dispose()
Me.lsvTest.LargeImageList = Nothing
Me.lsvTest.Items(Me.lsvTest.Items.Count - 1).Text = "Disposed"

REM Bitmap file remains locked.
objInfo.Open(IO.FileMode.Open).Close() ' Gives exception.

Why do the bitmap files remain locked?

Thanks
 
K

Ken Tucker [MVP]

Hi,

Use image.fromstream to keep the file from being locked.

Dim fsImage As New IO.FileStream("C:\Test.jpg", IO.FileMode.Open)

Dim objImage As Image = Image.FromStream(fsImage)

fsImage.Close()



Ken

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

Hello,

I have a listview with an image list. After dispoing the images and the
image list, the bitmap files remain locked:

REM Create.
objImage = Image.FromFile(strBmp)
If Me.lsvTest.LargeImageList Is Nothing Then
Me.lsvTest.LargeImageList = New ImageList()
End If
Me.lsvTest.LargeImageList.Images.Add(objImage)
objLVI = Me.lsvTest.Items.Add(strBmp)
objLVI.ImageIndex = Me.lsvTest.LargeImageList.Images.Count - 1
objImage = Nothing

REM Delete.
For Each objImage In Me.lsvTest.LargeImageList.Images
objImage.Dispose()
objImage = Nothing
Next
Me.lsvTest.LargeImageList.Images.Clear()
Me.lsvTest.LargeImageList.Dispose()
Me.lsvTest.LargeImageList = Nothing
Me.lsvTest.Items(Me.lsvTest.Items.Count - 1).Text = "Disposed"

REM Bitmap file remains locked.
objInfo.Open(IO.FileMode.Open).Close() ' Gives exception.

Why do the bitmap files remain locked?

Thanks
 
Q

Qwert

Yeah I tried it but it gives errors when I use that trick with
listview.imagelist. In other cases it works.
 
Q

Qwert

Turned out if you use the 'stream' trick to load graphics files for
list.imagelist, and you set the 'TransparentColor' property of the
ImageList, you get "Out of memory" exceptions. If you don't use that
property, things work. Have no idea why this happens.
 
H

Herfried K. Wagner [MVP]

Ken,

Ken Tucker said:
Use image.fromstream to keep the file from being locked.

Dim fsImage As New IO.FileStream("C:\Test.jpg", IO.FileMode.Open)

Dim objImage As Image = Image.FromStream(fsImage)

fsImage.Close()

.... notice that the stream must be kept open as long as the 'Image' object
is not disposed.
 
Top