views:

39

answers:

1

I have a MemoryStream of 10K which was created from a bitmap of 2MB and compressed using JPEG. Since MemoryStream can’t directly be placed in System.Windows.Controls.Image for the GUI, I am using the following intermediate code to convert this back to BitmapImage and eventually System.Windows.Controls.Image.

            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            BitmapImage imageSource = new BitmapImage();
            s.SlideInformation.Thumbnail.Seek(0, SeekOrigin.Begin);
            imageSource.BeginInit();
            imageSource.CacheOption = BitmapCacheOption.OnDemand; 
            imageSource.CreateOptions = BitmapCreateOptions.DelayCreation;
            imageSource.StreamSource = s.SlideInformation.Thumbnail;
            imageSource.EndInit();
            imageSource.Freeze();
            image.Source = imageSource;
            ret = image.Source;

My question is, when I store this in BitmapImage, the memory allocation is taking around 2MB. Is this expected? Is there any way to reduce the memory?

I have around 300 thumbnails and this converstion takes around 600MB, which is very high.

Appreciate your help!

+1  A: 

Is there any way to reduce the memory?

Yes their is: Don't create your memorystream from the image itself, instead use a thumbnail of it.

Here is a sample code of how to do it:

        private void button1_Click(object sender, EventArgs e)
            {
                Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                Bitmap myBitmap = new Bitmap(@"C:\Documents and Settings\Sameh\My Documents\My Pictures\Picture\Picture 004.jpg"); //3664 x 2748 = 3.32 MB
                Image myThumbnail = myBitmap.GetThumbnailImage(myBitmap.Width / 100, myBitmap.Height / 100 , myCallback, IntPtr.Zero); 
    //now use your thumbnail as you like
                myThumbnail.Save(@"C:\Documents and Settings\Sameh\My Documents\My Pictures\Picture\Thumbnail 004.jpg");
                //the size of the saved image: 36 x 27 = 2.89 KB
//you can create your memory stream from this thumbnail now
            }

            public bool ThumbnailCallback()
            {
                return false;
            }

And here is more details about the solution.

Sameh Serag
Samesh, Thanks for your reply.. I am not familiar with GetThumbnailImage(). Are there any disadvantage of using it? Do I get to see whole image?
Vin
The only disadvantage I can see is the use of the callback method which always return false! but this is how the sample given by Microsoft works!!! please, refer to the provided link for more details. As for the whole image: you will see a **Thumbnail** of the image; i.e. a scaled-down version of the **whole** image. Please, read the remarks section in the provided link, it is important.
Sameh Serag