来自BitmapSource的MemoryStream,需要减少内存消耗

时间:2010-05-21 02:31:42

标签: wpf memory wpf-controls memorystream bitmapimage

我有一个10K的MemoryStream,它是从2MB的位图创建的,并使用JPEG压缩。由于MemoryStream无法直接放在System.Windows.Controls.Image中,因此我使用以下中间代码将其转换回BitmapImage并最终转换为System.Windows.Controls.Image

我的问题是,当我将其存储在BitmapImage中时,内存分配正在进行中 2MB。这是预期的吗?有没有办法减少记忆?

我有大约300个缩略图,这个转换大约需要600MB,这非常高。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

  

有没有办法减少记忆?

是的,他们是:不要从图像本身创建内存流,而是使用它的缩略图。

以下是如何操作的示例代码:

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

here is more details关于解决方案。

相关问题