I am working on an application that includes a feature where a
directory is spidered and thumbnails are displayed. I have it pretty
much down, but when the bitmaps are loaded from file, memory is hogged
like crazy. I know this is where the memory problems occur because
I've gone through that whole comment/check the ram thing. Here's the
source of where the problem occurs:
Public Shared Sub FillImageList(ByVal dir As String, ByVal ListView As
ListView)
If Directory.Exists(dir) Then
'Get the files in an array and
'figure out how many there are
Dim file As String
Dim files() As String = Directory.GetFiles(dir)
Dim cleanfiles(files.Length) As String
Dim i As Integer = 0
For Each file In files
If Regex.IsMatch(file.ToString,
"[^<.*>.*](.jpg|.JPG|.jpeg|.JPEG|.png|.PNG|.gif|.GIF|.tif|.T IF|.bmp|.BMP)")
Then
cleanfiles(i) = file.ToString
i = i + 1
End If
Next
'Create a thumbnail for every image and add it to the
imagelist
If cleanfiles.Length > 0 And cleanfiles(0) <> "" Then
Dim h As Integer = 0
For Each file In cleanfiles
If file <> "" Then
'###BEGIN MEMORY LEAK
Dim objImage As Image
' Open the image
objImage = New Bitmap(file)
'###END MEMORY LEAK
' Convert the image to a thumbnail. This is
faster
' than the longer convert/compress to JPEG I
was
' using before.
objImage = objImage.GetThumbnailImage(100,
100, Nothing, New IntPtr)
'Add the thumbnail to the image list
ListView.LargeImageList.Images.Add(objImage)
'Add the filename as the text
ListView.Items.Add(file.ToString)
'Attach the image to the text
ListView.Items(h).ImageIndex = h
h = h + 1
objImage.Dispose()
End If
Next
End If
End If
End Sub
Any help would be very greatly appreciated. Reply here or email
pu****@putnamplanet.com. Thanks!
-C