【发布时间】:2012-11-28 21:16:36
【问题描述】:
这听起来可能很愚蠢,但是哪一种是最有效的加载图像的方法?
一个
BitmapImage bmp = new BitmapImage();
using(FileStream fileStream = new FileStream(source_path, FileMode.Open))
{
bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.StreamSource = fileStream;
bmp.EndInit();
if (bmp.CanFreeze)
bmp.Freeze();
images.source = bmp;
}
B
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bmp.UriSource = new Uri(source_path);
bmp.EndInit();
if (bmp.CanFreeze)
bmp.Freeze();
images.Source = bmp;
我记得我在某处读到,从流中加载会完全禁用缓存。如果这是真的,这是否意味着从流中加载在内存管理方面更好?
【问题讨论】:
-
您最终需要处理
fileStream,以防万一。 -
你能用秒表测量吗?
-
@usr 是的。代码已更新。
-
@Reyn 在using 语句中实例化fileStream 比显式处理它更安全。
标签: wpf image performance