.NET ImageList实现中的ImageList容量错误? [英] ImageList capacity bug in .NET ImageList implementation?

查看:159
本文介绍了.NET ImageList实现中的ImageList容量错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.net应用程序中为ListView使用ImageList时遇到了一些问题。在我的实现中,这个ImageList可以根据用途保存几千个图像。这个问题最初是在我尝试使用大量图像时出现的。一旦我越过5K +图像,我就会遇到一个通用的图像无法添加到ImageList异常,试图添加图像。发生此故障的确切数字略微上升。

I am having some problems utilizing an ImageList for my ListView in a .net application. In my implementation, this ImageList can hold a few thousand images depending on use. The issue originally arose when I was trying to work with a large number of images. Once I was getting past 5K+ images, I was running into a generic "Image cannot be added to the ImageList" exception trying to add an image. The exact number where this failure occurred jumped around a little bit.

因此,在尝试解决此问题时,我尝试使用本机ImageList_SetImageCount扩展ImageList的容量功能。那很好,解决了我的直接问题。我不得不使用ImageList [index] = image而不是.Add()方法填充列表。
这一切都很好但是当把图像拉出来的时候,问题就出现了。现在,无论何时我通过索引在ImageList中引用一个图像,我都会得到一个Out of Memory Exception。位图x = ImageList [任何索引]都会崩溃。

So, in trying to solve this issue I've tried to expand the capacity of the ImageList using native ImageList_SetImageCount function. That was fine and solved my immediate problem. I had to populate the list using the ImageList[index] = image instead of the .Add() method. This was all fine but when it came time to pulling an image out, the problems crept in. Now, anytime I reference an image in that ImageList by index I get an Out of Memory Exception. Bitmap x = ImageList[any index] would crash.

我试过追逐这么多不同的方法,但仍遇到ImageList的问题。

I've tried chasing this many different ways and am still encountering issues with the ImageList.

在我最后一次尝试进一步探讨这个问题时,我把下面的测试代码放在一起,它仍然表现得很奇怪!

In my last attempt to explore the issue further, I put together the following piece of test code which is still behaving odd!

    private void Form1_Load(object sender, EventArgs e)
    {
        list = new ImageList();
        list.ImageSize = new Size(128, 128);
        list.ColorDepth = ColorDepth.Depth32Bit;
        Image[] images = new Image[10];
        for (int y = 0; y < 10; y++)
        {
            images[y] = new Bitmap(@"Path to loading_photo.png");
        }
        for (int x = 0; x < 750; x++)
        {
            list.Images.AddRange(images);
        }

        list.Images[12] = new Bitmap(@"Path to another.png");

最后一行因无法将图像添加到ImageList错误而崩溃。奇怪的是,将第二个循环设置为较低的迭代(即200个,总共添加了2000个图像),它表现得很好。

the last line crashes with "Image cannot be added to the ImageList" error. Oddly, setting the second loop to lower iteration (i.e. 200 for a total of 2000 images being added) it behaves just fine.

在ImageList中是否有一定的阈值。网我不知道?任何帮助将不胜感激。

Is there a certain threshold on ImageList in .net I am not aware of? Any help would be greatly appreciated.

推荐答案

我无法通过发布的代码获得任何回复。然而,重要的是要意识到ImageList会生成位图的副本。所以你应该在添加后调用位图的Dispose()方法。如果你不这样做,那么你将面临一个严重的风险,就是当你的垃圾收集器经常运行不足以在你之后清理时,你会耗尽非托管内存,由位图数据消耗并致命。这不适用于代码段,因为只有10个位图。

I can get no repro whatsoever with the posted code. It is however important to realize that ImageList makes a copy of the bitmap. So you should call the bitmap's Dispose() method after adding it. If you don't then there's a serious risk that you'll run out of unmanaged memory, consumed by the bitmap data and fatal when the garbage collector doesn't run often enough to clean up after you. This doesn't apply to the snippet since there are only 10 bitmaps.

这篇关于.NET ImageList实现中的ImageList容量错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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