ImageSource.SetSource(MemoryStream) memory leak?
Last post 05-08-2008 9:28 PM by Allen Chen – MSFT. 3 replies.
Sort Posts:
05-06-2008 1:07 PM
ImageSource.SetSource(MemoryStream) memory leak?

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);
                }
            }
raulgspan

Joined on 03-29-2008
Posts 9
05-08-2008 12:07 AM
Re: ImageSource.SetSource(MemoryStream) memory leak?

Hi:

  It looks like normal. jpeg is compressed. When used it'll be decompressed. You can open that jpg file using Microsoft Paint, save it as 24-bit Bitmap and compare the size.

Regards

 

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Allen Chen – MSFT

Joined on 03-16-2007
Posts 717
05-08-2008 9:19 AM
Re: ImageSource.SetSource(MemoryStream) memory leak?

 Good point, thank you! So... is there an alternative way to display a ~250KB jpeg in Silverlight that avoids conversion to bitmap? Thanks again--

raulgspan

Joined on 03-29-2008
Posts 9
05-08-2008 9:28 PM
Marked as Answer
Re: ImageSource.SetSource(MemoryStream) memory leak?

Hi:

  As far as I know there's no way to do so. Jpeg is the format commonly used for storing and transmitting images. You can learn more about jpeg here:

http://en.wikipedia.org/wiki/JPEG

Regards

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Allen Chen – MSFT

Joined on 03-16-2007
Posts 717