简体   繁体   English

如何存储大量的WriteableBitmap?

[英]How to store a lot of WriteableBitmap?

I'm working with a set of 400+ images, that I need to modify in different ways. 我正在处理400张以上的图像,需要以不同的方式进行修改。 At the end, I need to have almost 1500 images. 最后,我需要拥有近1500张图像。 Let's say they all are 512*512px 假设它们都是512 * 512px

I want to first apply this modifications in order to get my 1500 images, and keep all this images in a List, to have a quick access on it in my application (I want to be able to switch between images without any loading time) 我想首先应用此修改,以获取1500张图像,并将所有这些图像保存在列表中,以便在应用程序中快速访问它(我希望能够在没有任何加载时间的情况下在图像之间进行切换)

To apply my modifications, I use WriteableBitmapEx. 要应用我的修改,我使用WriteableBitmapEx。

Thing is, to be able to modify it, I need to render this images into WriteableBitmap, and it gives me OutOfMemoryException. 事情是,为了能够对其进行修改,我需要将此图像渲染到WriteableBitmap中,这给了我OutOfMemoryException。

Here is a simplified example of what I'm doing: 这是我正在做的简化示例:

        List<WriteableBitmap> myList = new List<WriteableBitmap>();

        foreach (var image in mySetOfImages) // iterating on my set of 400+ images
        {
            WriteableBitmap source = image.RenderImage().As<WriteableBitmap>();

            WriteableBitmap dest1 = BitmapFactory.New(512, 512);
            WriteableBitmap dest2 = BitmapFactory.New(512, 512);

            [...] // Some lines modifying dest1 and dest2 using source

            myList.Add(dest1);
            myList.Add(dest2);
        }

I read a lot about this exception. 我阅读了很多有关此异常的信息。 I read that I could add 我读到我可以添加

GC.Collect();

may be inside my 'foreach', but I think this is going to take to much time to load. 可能在我的“ foreach”中,但是我认为这将花费很多时间来加载。

What I read also made me think that may be I'm not doing it the right way and that I should think of another way to do it. 我读过的书也让我觉得可能是我没有正确地做这件事,应该考虑另一种做事的办法。 That is why I'm posting here, do you guys have any tips? 这就是为什么我要在这里发布消息,你们有什么建议吗?

Basically, you just cant store more than 2GB of stuff in memory on a 32bit .NET process. 基本上,您不能在32位.NET进程中将超过2GB的内容存储在内存中。 As pointed out, this isn't a Garbage Collector (GC) issue, as you are strongly referencing all these images by storing them in a List<T> . 如前所述,这不是垃圾收集器(GC)的问题,因为您通过将它们存储在List<T>来强烈引用所有这些图像。 The Garbage Collector will allocate memory for each of your images, and when it is "under pressure" will try to find objects that are no longer reference, and then release them. 垃圾收集器将为每个图像分配内存,当它处于“压力之下”时,它将尝试查找不再引用的对象,然后释放它们。 However as you only appear to be adding to the list, the pressure will only increase :-( 但是,由于您似乎只是添加到列表中,因此压力只会增加:-(

You have two options, from what I can see: 从我所见,您有两个选择:

  1. Find a way to reduce the amount of memory each image is requires to allocated. 寻找一种减少每个图像需要分配的内存量的方法。 Most obvious is to reduce the size eg only cache a thumbnail. 最明显的是减小大小,例如仅缓存缩略图。 And second is to not cache 2 images for each source image! 第二个是不要为每个源图像缓存2个图像!
  2. Try to lazily load the images only when they are requested, and potentially drop unused (out of view) images from the list. 尝试仅在请求时才延迟加载图像,并可能从列表中删除未使用的(视线外)图像。 Now you are into creating your own WPF Visualization code for scrolling list etc. 现在您要创建自己的WPF可视化代码以滚动列表等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM