wpf image控件 显示不同图片时 释放文件内存流

public static BitmapImage LoadImageFreeze(string imagePath)
{
try
{
bitmap = new BitmapImage();
if (File.Exists(imagePath))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;

using (Stream ms = new MemoryStream(File.ReadAllBytes(imagePath)))
{
bitmap.StreamSource = ms;
bitmap.EndInit();
bitmap.Freeze();
}
}
return bitmap;
}
catch (Exception)
{
return null;
}
}

至少证明我们还活着
原文地址:https://www.cnblogs.com/pengde/p/10445569.html