May be this sound stupid but, which one is the most efficient way to load image?
A
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;
I remember I read somewhere that loading from a stream completely disables the cache. If thats true, does it means loading from a stream is better in term on memory management?
As far as i have understood, when you load a BitmapImage by setting its
UriSourceproperty, the image is always cached. I don’t know of any way to avoid this. At least settingBitmapCreateOptions.IgnoreImageCacheonly ensures that an image is not retrieved from the cache, but it does not prevent that the image is stored in the cache.The “Remarks” in BitmapCreateOptions says that
My conclusion from this is that caching is only performed when an image is loaded by an Uri. In other words, if you really need to inhibit image caching, you will have to load an image by its
StreamSourceproperty.However, if this is really “better in terms of memory management” is perhaps worth an experiment. You could try out both alternatives and see if you observe any significant difference in memory consumption.