使用 WPF Image 控件渲染时,约 11MB 大小的图像文件会占用大量内存

发布于 2024-07-25 23:34:59 字数 259 浏览 7 评论 0原文

当我尝试将 WPF 图像的源设置为约 11MB 大小并在 14 MeagaPixcel 相机中拍摄的图像文件时,当图像在屏幕上渲染时,内存会飙升至约 170 MB,并且之后内存也不会下降渲染。 如果我尝试使用 .Net 2.0 Picturebox 控件执行相同操作,则所使用的内存仅为 0.5MB 到 1MB。 从逻辑上讲,如果图像的文件大小为 11MB,那么渲染时它最多只占用 11MB,对吗? WPF 中出现这种行为的原因是什么? 在屏幕上渲染完成后,有什么方法可以处理多余的内存垃圾吗?

When i try to set the Source of WPF Image to a Image file that is ~11MB size and Shot in 14 MeagaPixcel Camera, the memory shoots up to around 170 MB when the image is rendered on the screen and the memory also never comes down after Rendering.
If i try to do the same using .Net 2.0 Picturebox control the memory utilized is only .5MB to 1MB.
Logically if the file size of an image is 11MB then it should at the maximum occupy only 11MB while rendering right? What is the cause of such behaviour in WPF? and is there any way to dispose the extra junk of memory after the rendering is compeleted on the screen?

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

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

发布评论

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

评论(1

活雷疯 2024-08-01 23:34:59

回答你问题的第一部分:

数码相机拍摄的图像存储为 jpg 文件,因此被压缩。 当读入内存时,它将被解压缩。 这就是您在这里看到的大小差异的原因。

例如,在 Canon EOS 450 上拍摄的照片在磁盘上的文件大小为 3 MB。 它的尺寸为 3072 x 2048。这导致内存大小为 3072 * 2048 像素 * 24 位/像素 = 18,874,368 字节(这是否有意义 - 我从来没有 100% 确定这些计算)

直到保存图像数据的对象超出范围并被垃圾收集清除后,内存使用量才会下降。

例如,您将需要类似于此代码的内容:

using (Image image = Image.FromFile(imageName))
{
    // Non property item properties
    FileName = imageName;
    PixelFormat = image.PixelFormat;
    Width = image.Size.Width;
    Height = image.Size.Height;

    foreach (PropertyItem pi in image.PropertyItems)
    {
        EXIFPropertyItem exifpi = new EXIFPropertyItem(pi);
        this.propertyItems.Add(exifpi);
    }
}

一旦我从图像中获取了所需的所有信息,using 语句将允许垃圾收集启动并释放内存。

To answer the first part of your question:

Images shot on digital cameras are stored as jpg files and hence compressed. When read into memory it will be uncompressed. This accounts for the difference in size you are seeing here.

For example a photo shot on a Canon EOS 450 has a file size on disk of 3 MB. It's dimensions are 3072 x 2048. This leads to a size in memory of 3072 * 2048 pixels * 24 bits / pixel = 18,874,368 bytes (does that make sense - I'm never 100% sure of these calculations)

The memory usage won't come down until the object holding the image data falls out of scope and is cleared by the garbage collection.

For example you'll need something along the lines of this code:

using (Image image = Image.FromFile(imageName))
{
    // Non property item properties
    FileName = imageName;
    PixelFormat = image.PixelFormat;
    Width = image.Size.Width;
    Height = image.Size.Height;

    foreach (PropertyItem pi in image.PropertyItems)
    {
        EXIFPropertyItem exifpi = new EXIFPropertyItem(pi);
        this.propertyItems.Add(exifpi);
    }
}

Once I've got all the information I need from the image the using statement allows the garbage collection to kick in and free the memory.

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