如何配置可写位图? (WPF) [英] How to dispose a Writeable Bitmap? (WPF)

查看:74
本文介绍了如何配置可写位图? (WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间,我发布了一个与WriteableBitmap内存泄漏有关的问题,尽管我收到了与该问题有关的精彩提示,但我仍然认为这是一个严重的错误/(我犯了一个错误)/(混乱)/(有些其他东西).

Some time ago i posted a question related to a WriteableBitmap memory leak, and though I received wonderful tips related to the problem, I still think there is a serious bug / (mistake made by me) / (Confusion) / (some other thing) here.

所以,这又是我的问题:

So, here is my problem again:

假设我们有一个带有图片和按钮的WPF应用程序.图片的来源是一个非常大的位图(3600 * 4800 px),当在运行时显示该图片时,应用程序会消耗约90 MB的空间.

Suppose we have a WPF application with an Image and a button. The image's source is a really big bitmap (3600 * 4800 px), when it's shown at runtime the applicaton consumes ~90 MB.

现在假设我希望从图像(真正的大图像)的源实例化一个WriteableBitmap,当这种情况发生时,应用程序将消耗约220 MB的内存.

Now suppose i wish to instantiate a WriteableBitmap from the source of the image (the really big Image), when this happens the applications consumes ~220 MB.

现在是棘手的部分,当对图像的修改(通过WriteableBitmap)结束,并且对WriteableBitmap的所有引用(至少是我所知道的那些引用)都被销毁(在方法的末尾或通过将它们设置为null),应释放writeableBitmap所使用的内存,并且应用程序使用量应返回到约90 MB.问题是有时返回,有时不返回.

Now comes the tricky part, when the modifications to the image (through the WriteableBitmap) end, and all the references to the WriteableBitmap (at least those that I'm aware of) are destroyed (at the end of a method or by setting them to null) the memory used by the writeableBitmap should be freed and the application consumption should return to ~90 MB. The problem is that sometimes it returns, sometimes it does not.

这是示例代码:

// The Image's source whas set previous to this event
private void buttonTest_Click(object sender, RoutedEventArgs e)
    {
        if (image.Source != null)
        {
            WriteableBitmap bitmap = new WriteableBitmap((BitmapSource)image.Source);

            bitmap.Lock();

            bitmap.Unlock();

            //image.Source = null;
            bitmap = null;
        }
    }

如您所见,引用是本地的,并且应该在方法结束时(或在垃圾回收器决定这样做时)释放内存.但是,该应用可能要消耗约224 MB的空间,直到宇宙结束为止.

As you can see the reference is local and the memory should be released at the end of the method (Or when the Garbage collector decides to do so). However, the app could consume ~224 MB until the end of the universe.

任何帮助都会很棒.

推荐答案

是否需要以相同的分辨率和像素渲染位图图像?您可以在低得多的像素集处创建writeablebitmap,然后调用render方法.由于在调用render时writeablebitmap包含对原始uielements的引用,因此在这种情况下,您将具有3个块:1)原始uielement,2)writeablebitmap中的像素,3)对复制的原始文件的引用.

Is it necessary to render the Bitmap image at the same resolution and pixels? You could create the writeablebitmap at a much lower set of pixels and call the render method. Since the writeablebitmap carries a reference to the original uielements when calling render, in this case, you are going to have 3 chunks: 1) original uielement, 2) pixels in writeablebitmap, 3) reference to copied original.

就内存泄漏而言,我的writeablebitmap也存在类似的问题,并且我通过检查此链接对其进行了修复:

I had a similar issue with the writeablebitmap in terms of memory leaks and I fixed it from checking out this link: http://www.wintellect.com/CS/blogs/jprosise/archive/2009/12/17/silverlight-s-big-image-problem-and-what-you-can-do-about-it.aspx

如果您创建另一个writeablebitmap并将像素复制过来,然后处置第一个writeablebitmap,您应该会看到一些内存释放-至少在我的情况下如此.

If you create another writeablebitmap and copy the pixels over, then dispose of the first writeablebitmap you should see some memory release - at least I did in my scenario.

这篇关于如何配置可写位图? (WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆