VB.NET ImageList 内存不足问题

VB.NET Out of Memory Issue on ImageList

已编辑: 即使我将 dispose()End Using 将位图添加到 ImageList 后。

这是我的代码。

Function loadImage()
    Dim item As New ListViewItem

    imageList.ImageSize = New Size(45, 70)

    For Each value In arr
        If System.IO.File.Exists(value) Then
            Using img As New Bitmap(value)
                imageList.Images.Add(Image.FromHbitmap(img.GetHbitmap))
            End Using
            newListView.LargeImageList = imageList
            item = New ListViewItem(value)
            newListView.Items.Add(item)
            item.Name = value
            item.Tag = System.IO.Path.GetDirectoryName(value)
            newListView.Items(item.Index).ImageIndex = item.Index
        End If
    Next

    newListView.View = View.LargeIcon
    Return Nothing
End Function

我在 arr 上有 96 个值由图像路径组成,但只有 82 个值被显示然后发生 OOM 错误。

也许我误用了 Using 声明或其他任何内容。我希望你能帮我解决这个问题。谢谢!

[已解决] 创建图像副本并将复制图像的大小调整为位图缩略图大小,然后将其添加到 ImageList()。添加后,处理原图和位图副本。

我会post代码来帮助有同样问题的人。

    Dim item As New ListViewItem

    imageList.ImageSize = New Size(80, 100)

    For Each value In arr
        If System.IO.File.Exists(value) Then
            Dim buffer As Byte() = File.ReadAllBytes(value)
            Dim stream As MemoryStream = New MemoryStream(buffer)

            Dim myBitmap As Bitmap = CType(Bitmap.FromStream(stream), Bitmap)
            Dim pixelColor As Color = myBitmap.GetPixel(50, 80)
            Dim newColor As Color = Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B)

            myBitmap.SetPixel(0, 0, newColor)
            myBitmap = myBitmap.GetThumbnailImage(80, 100, Nothing, IntPtr.Zero)

            imageList.Images.Add(Image.FromHbitmap(myBitmap.GetHbitmap))

            myBitmap.Dispose()
            stream.Dispose()

            newListView.LargeImageList = imageList
            item = New ListViewItem(value)
            newListView.Items.Add(item)

            newListView.Items(item.Index).ImageIndex = item.Index
        End If
    Next

    newListView.View = View.LargeIcon

其中 arr 是图像路径目录的列表。