C# WPF 在不使用 FileStream 的情况下释放 BitmapImage
C# WPF Release BitmapImage without using FileStream
我的问题:有没有一种方法可以将图像加载到 BitmapImage 中,既不会占用大量内存,又可以删除图像?阅读下文了解更多详情:
我有一个 class PhotoCollection : ObservableCollection{ },其中照片 class 创建一个 BitmapImage 对象:
图片集Class:
public class PhotoCollection : ObservableCollection<Photo>
{
...Stuff in here...
}
照片 Class:
public class Photo
{
public Photo(string path)
{
_path = path;
_source = new Uri(path);
BitmapImage tmp = new BitmapImage();
tmp.BeginInit();
tmp.UriSource = _source;
tmp.CacheOption = BitmapCacheOption.None;
tmp.DecodePixelWidth = 200;
tmp.DecodePixelHeight = 200;
tmp.EndInit();
BitmapImage tmp2 = new BitmapImage();
tmp2.BeginInit();
tmp2.UriSource = _source;
tmp2.CacheOption = BitmapCacheOption.None;
tmp2.EndInit();
_image = BitmapFrame.Create(tmp2, tmp);
_metadata = new ExifMetadata(_source);
}
public BitmapFrame _image;
public BitmapFrame Image { get { return _image; } set { _image = value; } }
...More Property Definitions used to support the class
}
当我将计算机上的图像拖放到列表框中时,照片将加载到照片的照片集并显示在列表框中(感谢绑定)。如果我删除 50MB 的照片,我的程序会占用大约 50MB 的内存。
我的问题是我需要稍后从文件夹中删除这些照片。为此,我必须首先卸载或处理内存中的照片,因为 BitmapImage 会锁定文件。我不知道该怎么做。
找到这个相似的Whosebug Question我想我的问题都解决了。实施 Whosebug 问题中的代码:
public class Photo
{
public Photo(string path)
{
BitmapImage tmp = new BitmapImage();
BitmapImage tmp2 = new BitmapImage();
tmp = LoadImage(_path);
tmp2 = LoadImage(_path);
...
}
private BitmapImage LoadImage(string myImageFile)
{
BitmapImage myRetVal = null;
if (myImageFile != null)
{
BitmapImage image = new BitmapImage();
using (FileStream stream = File.OpenRead(myImageFile))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
myRetVal = image;
}
return myRetVal;
}
...
}
实施 FileStream 以将图像加载到 BitMapImage 对象时,只有一个 巨大 问题。我的内存使用率暴涨!比如 50MB 的照片占用了 1GB 的内存并且加载时间延长了 10 倍:
Link to Image
重申一下我的问题:有没有办法将图像加载到 BitmapImage 中,不会占用大量内存并且图像仍然可以删除?
非常感谢! ^_^
您可以设置 BitmapImage
的 DecodePixelWidth
和 DecodePixelHeight
属性来告诉它加载到内存中的像素更少。
我的问题:有没有一种方法可以将图像加载到 BitmapImage 中,既不会占用大量内存,又可以删除图像?阅读下文了解更多详情:
我有一个 class PhotoCollection : ObservableCollection
图片集Class:
public class PhotoCollection : ObservableCollection<Photo>
{
...Stuff in here...
}
照片 Class:
public class Photo
{
public Photo(string path)
{
_path = path;
_source = new Uri(path);
BitmapImage tmp = new BitmapImage();
tmp.BeginInit();
tmp.UriSource = _source;
tmp.CacheOption = BitmapCacheOption.None;
tmp.DecodePixelWidth = 200;
tmp.DecodePixelHeight = 200;
tmp.EndInit();
BitmapImage tmp2 = new BitmapImage();
tmp2.BeginInit();
tmp2.UriSource = _source;
tmp2.CacheOption = BitmapCacheOption.None;
tmp2.EndInit();
_image = BitmapFrame.Create(tmp2, tmp);
_metadata = new ExifMetadata(_source);
}
public BitmapFrame _image;
public BitmapFrame Image { get { return _image; } set { _image = value; } }
...More Property Definitions used to support the class
}
当我将计算机上的图像拖放到列表框中时,照片将加载到照片的照片集并显示在列表框中(感谢绑定)。如果我删除 50MB 的照片,我的程序会占用大约 50MB 的内存。
我的问题是我需要稍后从文件夹中删除这些照片。为此,我必须首先卸载或处理内存中的照片,因为 BitmapImage 会锁定文件。我不知道该怎么做。
找到这个相似的Whosebug Question我想我的问题都解决了。实施 Whosebug 问题中的代码:
public class Photo
{
public Photo(string path)
{
BitmapImage tmp = new BitmapImage();
BitmapImage tmp2 = new BitmapImage();
tmp = LoadImage(_path);
tmp2 = LoadImage(_path);
...
}
private BitmapImage LoadImage(string myImageFile)
{
BitmapImage myRetVal = null;
if (myImageFile != null)
{
BitmapImage image = new BitmapImage();
using (FileStream stream = File.OpenRead(myImageFile))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
myRetVal = image;
}
return myRetVal;
}
...
}
实施 FileStream 以将图像加载到 BitMapImage 对象时,只有一个 巨大 问题。我的内存使用率暴涨!比如 50MB 的照片占用了 1GB 的内存并且加载时间延长了 10 倍:
Link to Image
重申一下我的问题:有没有办法将图像加载到 BitmapImage 中,不会占用大量内存并且图像仍然可以删除?
非常感谢! ^_^
您可以设置 BitmapImage
的 DecodePixelWidth
和 DecodePixelHeight
属性来告诉它加载到内存中的像素更少。