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

发布于 2024-09-02 04:56:21 字数 367 浏览 10 评论 0原文

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

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

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

感谢您的帮助!

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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫漫岁月 2024-09-09 04:56:21

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

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

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

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

以下是有关该解决方案的更多详细信息

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文