I'm having memory issues reading and closing multiple JPG images.
I'm loading JPG files into an image object, reading the file information (size, dimensions, date, etc.), and reading their EXIF data to get the geocode for Lat/Long. Then I'm pushing the information to a ListView object. This is all working great on folders with up to 60 or so JPG's. Once I go past a certain number (not sure what the threshold actually is) I get my favorite error...
************** Exception Text ************** System.OutOfMemoryException: Out of memory. at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at System.Drawing.Image.FromFile(String filename)
...etc.
I'm leaking memory somewhere, but I'm a bit of a novice with .NET applications. I'm calling this routine from a loop which reads through a collection of JPG files in a directory.
Public Function JPGDat(ByRef jpg As FileInfo) As ListViewItem
Dim filEntry(6) As String
Dim lstEntry As ListViewItem = Nothing
Dim pic As Image = Image.FromFile(jpg.FullName) 'HERE IS WHERE WE CRASH
filEntry(0) = jpg.Name
filEntry(1) = (Math.Round(jpg.Length / 1024)).ToString() 'File Size
filEntry(2) = Format(pic.PhysicalDimension.Width, "0") 'Pixel width dimension
filEntry(3) = Format(pic.PhysicalDimension.Height, "0") 'Pixel height dimension
Try
Dim CLatt As Double = GetCoord(pic.GetPropertyItem(2)) 'Get Latitude from EXIF
Dim CLong As Double = GetCoord(pic.GetPropertyItem(4)) 'Get Longitude from EXIF
filEntry(4) = Format(CLatt, "0.000000000000000")
filEntry(5) = Format(CLong, "0.000000000000000")
Catch ex As Exception
filEntry(4) = ""
filEntry(5) = ""
End Try
filEntry(6) = "Date: " + Format(jpg.CreationTime, "yyyy-MM-dd")
lstEntry = New ListViewItem(filEntry)
If filEntry(4) <> "" Then
lstEntry.Checked = True
End If
pic.Dispose()
Return lstEntry
End Function