WPF位图图像不';不处置

WPF位图图像不';不处置,wpf,filestream,idisposable,Wpf,Filestream,Idisposable,我已经从filestream数据库中加载了一个图像列表。当我最初加载它们时,内存跳转并没有那么大。当我在屏幕上显示它们时,我的内存使用量会急剧增加,即使在我处理BitmapImage streamsource并将其设置为Nothing之后,内存也不会消失 Dim newItem As New MIdentifiedImage Dim data As Byte() = dt.Rows(i).Item("ScannedImage") Dim strm As New Memor

我已经从filestream数据库中加载了一个图像列表。当我最初加载它们时,内存跳转并没有那么大。当我在屏幕上显示它们时,我的内存使用量会急剧增加,即使在我处理BitmapImage streamsource并将其设置为Nothing之后,内存也不会消失

    Dim newItem As New MIdentifiedImage
    Dim data As Byte() = dt.Rows(i).Item("ScannedImage")
    Dim strm As New MemoryStream(data)

    Dim bi As New BitmapImage()
    bi.BeginInit()
    bi.StreamSource = strm
    bi.EndInit()
    bi.Freeze()
    newItem.ScannedImage = bi
这是我的代码

  For Each img In InvoiceObj.ImageList
    img.ScannedImage.StreamSource.Dispose()
    img.ScannedImage.StreamSource = Nothing

  Next

所以我的探查器说内存使用率很低,但当我在任务管理器中检查时,内存使用率相当高。如果我继续加载超过1.5GB内存的图像,它将停止在屏幕上显示图像。

您还需要清除
图像列表。仅将
StreamSource
设置为
null
不会导致删除已加载的数据。完成后,通过清除
ImageList
,您允许GC清理实际的
BitmapImage
实例。

除了Reed所说的,如果您在调用EndInit之前设置
bi.CacheOption=BitmapCacheOption.OnLoad
,您可以立即处理流,如图所示。