【问题标题】:MemoryStream from BitmapSource, need to reduce memory consumptionMemoryStream from BitmapSource,需要减少内存消耗
【发布时间】:2010-05-21 02:31:41
【问题描述】:

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

我的问题是,当我将其存储在 BitmapImage 中时,内存分配正在占用 2MB。这是预期的吗?有什么办法可以减少内存?

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

感谢您的帮助!

【问题讨论】:

    标签: wpf memory wpf-controls memorystream bitmapimage


    【解决方案1】:

    有什么办法可以减少内存?

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

    下面是一个示例代码:

            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关于解决方案。

    【讨论】:

    • Samesh,感谢您的回复。我对 GetThumbnailImage() 不熟悉。使用它有什么缺点吗?我可以看到整个图像吗?
    • 我能看到的唯一缺点是使用了总是返回假的回调方法!但这就是微软提供的样本的工作原理!!!请参阅提供的链接以获取更多详细信息。至于整个图像:您将看到图像的缩略图;即整个图像的缩小版本。请阅读所提供链接中的备注部分,这很重要。
    猜你喜欢
    • 2013-06-13
    • 1970-01-01
    • 2023-03-18
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多