我开发了一个应用程序来捕获图像并将其保存到数据库中,但是我遇到了内存使用问题。在我的域对象上,我有 3 个属性:
图片 - 字节数组,内容为 jpg
RealImageThumb - 转换为 BitmapImage 并缩小的字节数组,在网格视图中与其他缩略图一起显示给用户
RealImage - 没有设置器,将字节数组转换为位图源,当用户将鼠标悬停在它上面时,它会显示在工具提示中。
我遇到的问题是,如果用户依次将鼠标悬停在每个图像上,内存使用量就会呈螺旋式上升。我意识到当用户将鼠标悬停在生成位图源并且内存没有释放时,我尝试给 RealImage 一个支持属性并在之后将其分配给 null 但是内存没有被释放(等待垃圾集电极?)。
编辑:
这就是你说的雷?我没有得到如下工具提示中显示的任何内容,但是如果我尝试定义 a WeakReference<BitmapImage>
,则会收到 System.WeakReference 没有类型参数错误。
private WeakReference _realImage;
public virtual BitmapImage RealImage
{
get
{
if (_realImage == null || _realImage.Target == null)
{
if (Image == null) return null;
var newBitmapImage = new BitmapImage();
newBitmapImage.BeginInit();
newBitmapImage.CacheOption = BitmapCacheOption.None;
newBitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
newBitmapImage.StreamSource = new MemoryStream(Image);
newBitmapImage.EndInit();
_realImage = new WeakReference(newBitmapImage);
}
return (BitmapImage)_realImage.Target;
}
}