ImageList中/图像OutOfMemoryException异常 [英] ImageList / Image OutOfMemoryException

查看:179
本文介绍了ImageList中/图像OutOfMemoryException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个OutOfMemoryException获得来自一个ImageList我一直无法找到一个合适的解决问题的办法图像时的痛苦。

I'm suffering from an OutOfMemoryException when obtaining an Image from an ImageList I've been unable to find an appropriate solution to the problem.

我有自定义ListView控件,这已连接到它的事件ListViewItems的绘制。这就要求,其目的是绘制项目的静态方法。

I've got a Custom ListView control, which has attached to it an Event for the drawing of ListViewItems. This then calls a static method which is designed to draw the item.

有关的约300项一个ListView,我们得到的内存每次100MB左右蹿升ListView的滚动。有问题的代码已被追查到以下内容:

For a ListView of around 300 items, we're getting the memory jump up around 100Mb each time the ListView is scrolled. The offending code has been tracked down to the following:

Image image = item.ImageList.Images[item.ImageKey];
if (image != null)
{
    Size imageOffset = new Size((bounds.Width - image.Width) / 2, 2); 
    Point imagePosition = bounds.Location + imageOffset;
    graphics.DrawImageUnscaled(image, imagePosition);
}



看来(当然在WinXP)表示,垃圾收集工作不正常,导致内存螺旋。我们试过的代码来解决这个问题块之后直接添加image.Dispose(),但没有任何效果。

It seems (certainly on WinXP) that the garbage collection isn't working correctly, causing the memory to spiral. We've tried adding an image.Dispose() directly after the block of code to fix the issue, but that doesn't have any effect.

唯一的解决办法我设法到目前为止发现,在静态方法的末尾调用GC.Collect()。但这样做的问题在于,它然后导致ListView中慢慢重新绘制自己和你最终在屏幕上得到的文物,而它试图重新绘制。

The only solution I have managed to find so far, is at the end of the static method to call GC.Collect(). The problem with this however is that it then causes the ListView to re-paint itself slowly and you end up getting artifacts on the screen while it attempts to re-draw.

任何人都经历吗?一种解决办法还是知道?

Has anyone else experienced this? Or knows of a workaround?

推荐答案

您处理图形?此外,它在处置你的形象就像你提到的,那么你就需要确保它被取出的ImageList,或者你将造成更多的问题。什么是什么格式的图像?

Are you disposing graphics? Also, it you dispose your image like you mentioned then you would need to make sure it is taken out of the ImageList or you will cause more issues. What what format are the images?

在一般来说,当你的内存不足问题图像时参与,您的问题将是要么一些方法不喜欢一些图像格式,或9/10的时候,你误解了图形对象中的一个的生命周期。

In general when you get out of memory issues when images are involved, your issue will be either some method does not like some image format, or 9/10 times, you misunderstood the lifecycle of one of the graphic objects.


  • 检查所有的图形用法并把它们放在使用块。

  • 检查图片生命周期,并小心复制它们,处理它们,收盘底层流等。

  • 加载了一个内存管理器(VS2008有一个内置),看看有什么是没有得到清理很好。

  • Check all your Graphics usage and put them in using blocks.
  • Check your Image life cycle and be careful with copying them, disposing them, closing underlying streams, etc.
  • Load up a memory manager (VS2008 has one built in) and see what is not getting cleaned up nicely.

编辑:

下面是我能找到,使用最好的选择的 ImageList.Draw (图形,X,Y,宽度,高度,指数)。这将使用内部处理,而不是创建影像的副本。

Here is the best option I can find, use ImageList.Draw(graphics, x, y, width, height, index). This will use the internal handle instead of creating a copy of the image.

这篇关于ImageList中/图像OutOfMemoryException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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