If I do the following:
- Get byte array of jpeg data
- Instantiate a MemoryStream with this byte array
- Instantiate a System.Windows.Media.Imaging.BitmapImage
- Call BitmapImage.SetSource(MemoryStream)
The
image displays correctly, but the memory usage in my IE process
balloons by several MEGABYTES. The problem definitely seems dependent
on how large the jpeg file is; a small jpeg is fine, but a 250K jpeg
takes up ~10MB of memory. Is this a known issue?
Here's some code to replicate the problem:
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Importable Files (*.jpg, *.jpeg)|*.jpg;*.jpeg";
if (ofd.ShowDialog() == DialogResult.OK)
{
using (System.IO.Stream str = ofd.SelectedFile.OpenRead())
{
Byte[] bytes = new Byte[str.Length];
str.Read(bytes, 0, bytes.Length);
System.Windows.Media.Imaging.BitmapImage imgSource = new System.Windows.Media.Imaging.BitmapImage();
//THE LINE BELOW CAUSES SEEMING MEMORY ALLOCATION ISSUE
imgSource.SetSource(str);
}
}