加载BitmapSources时出现内存问题 [英] Memory Issues when loading BitmapSources
问题描述
我正在尝试为WPF应用程序创建一个简单的Photo-Viewer.实际上它已经存在并且可以正常工作了:)
I am trying to create a simple Photo-Viewer for my WPF application. Actually it already exists and works almost fine :)
查看器包含两个主要元素:
The viewer consists of two main elements:
1:一个列表框,显示选定目录中的所有图像
1: a ListBox, that shows all images within a selected directory
2:显示从列表框中选择的项目的图像
2: an Image that shows the selected item from the listbox
问题:当目录中包含大量图像时,应用程序内存不足.
Problem: When I have a large number of images within a directory the app runs out of memory.
详细信息:
列表框(1)-数据源是一个可观察到的集合,它包含如下对象,并且显示该对象的Thumbnail属性:
The listbox (1) -datasource is an observable collection wich contains objects like the following and it displays the Thumbnail property of the object:
public class ImageFile
{
private bool thumbnailLoaded = false;
private readonly string thumbnailPath;
private readonly int standardWidth;
public ImageFile(string path, string name = null, string thumbPath = null)
{
if (path == null)
throw new ArgumentException("Argument 'path' may not be null");
Path = name==null ? path : System.IO.Path.Combine(path, name);
Name = name;
thumbnailPath = thumbPath;
}
public ImageFile(BitmapFrame sourceFrame)
{
Image = sourceFrame;
standardWidth = 1600;
}
public void SetThumbnail()
{
if (Thumbnail != null) return;
if (thumbnailPath != null)
{
Thumbnail = ImageUtils.GetImage(thumbnailPath);
thumbnailLoaded = true;
}
}
public void RemoveThumbnail()
{
thumbnailLoaded = false;
Thumbnail = null;
}
public string Name { get; set; }
public BitmapFrame Image
{
get
{
if (image == null && !string.IsNullOrEmpty(Path))
{
var tmpImg = ImageUtils.GetBitmapFrame(Path);
Initialize(tmpImg);
return ImageUtils.GetImage(Path, standardWidth);
}
return image;
}
private set
{
if (value != null)
{
image = value;
Initialize(image);
image.Freeze();
}
}
}
public BitmapSource Thumbnail { get; private set; }
public String Path { get; private set; }
public BitmapMetadata Metadata { get; private set; }
public ReadOnlyCollection<ColorContext> ColorContexts { get; private set; }
private void Initialize(BitmapFrame tmpImage)
{
Metadata = (BitmapMetadata)tmpImage.Metadata;
ColorContexts = tmpImage.ColorContexts;
if (!thumbnailLoaded)
{
SetThumbnail(tmpImage);
}
}
}
public static BitmapFrame GetImage(string path, int resizedWidth = 0)
{
//No Metadata here :(
if (!string.IsNullOrEmpty(path))
{
var info = new FileInfo(path);
if (info.Exists && info.Length > 0)
{
var bi = new BitmapImage();
bi.BeginInit();
if(resizedWidth!=0)
bi.DecodePixelWidth = resizedWidth;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri(info.FullName);
bi.EndInit();
return BitmapFrame.Create(bi);
}
}
return null;
}
我遇到的问题是,即使我仅设置了缩略图那些可见的图像文件
The problem I am having is, that even though I only set the thumbnails for those ImageFiles that are visible
并在其他图像文件上调用Remove(),则内存消耗增加.
and call Remove() on the others, the memory consumption goes up.
仅当我开始在WPF中的图像组件(2)中编辑图像时,才调用图像"属性.
The property 'Image' is only called, when I start editing the image in the image component (2) in WPF.
(我不想使用私有字段将图像存储在此处,因为我希望节省内存)
(I do not use a private field to store the image there, because I hoped to save memory)
我该怎么办才能保持较低的内存使用率?我需要能够打开目录
What can I do to keep the memory usage low? I need to be able to open directories
,其中包含无限数量的图像.
containing an unlimited number ofimages.
任何帮助表示感谢.有没有更好的方法可以做到这一点?
Any Help appreciated. Is there a better way to achieve this?
谢谢
Jens
JEns D.
JEns D.
推荐答案
"任何帮助表示赞赏.有没有更好的方法来实现这一目标?"
"Any Help appreciated. Is there a better way to achieve this?"
WPF论坛.
这篇关于加载BitmapSources时出现内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!