I need to use memory stream because I load image from a database. When I use file stream, just to compare, footprint of the app is consistent with 1x size of uncompressed image. But if I use memory stream, memory usage is more like 3x. I used 200MB of png images (4 images) and the app took 1GB of ram.
Is there a way to reduce memory usage?
private BitmapImage readfromdsk(string s)
{
var photo2f = File.Open(s, FileMode.Open, FileAccess.Read);
byte[] b = null;
photo2f.Read(b=new byte[photo2f.Length], 0, (int)photo2f.Length);
var bitmapImage = new BitmapImage();
using (MemoryStream ms = new MemoryStream(b))
{
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = ms; //// lot of memory allocated on this line
bitmapImage.EndInit();
}
b = null;
photo2f.Dispose();
return bitmapImage;
}