WPF The Most Effective Image Upload Method

This sound may be silly, but which one is the most efficient way to upload an 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; } 

IN

 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 reading somewhere that loading from a stream completely disables the cache. If this is true, does this mean that loading from a stream is better in terms of memory management?

+4
source share
1 answer

As I understand it, when you load BitmapImage by setting its UriSource property, the image is always cached. I do not know how to avoid this. At the very least, setting BitmapCreateOptions.IgnoreImageCache ensures that the image will not be retrieved from the cache, but this will not prevent the image from being stored in the cache.

The Notes in BitmapCreateOptions says that

When IgnoreImageCache is selected, any existing entries in the cache image are replaced, even if they have the same Uri

My conclusion is that caching is only done when the image is loaded by Uri. In other words, if you really need to block image caching, you will have to load the image using its StreamSource property.

However, if it is truly β€œbetter in terms of memory management,” perhaps an experiment is worth it. You can try both alternatives and see if you observe any significant difference in memory consumption.

+1
source

All Articles