加载从流的图像不保持流开 [英] Loading an image from a stream without keeping the stream open

查看:204
本文介绍了加载从流的图像不保持流开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能使用System.Drawing.Image对象的FromStream方法,而不必保持流开放,图象的寿命

Is it possible to use the FromStream method of System.Drawing.Image without having to keep the stream open for the lifetime of the image?

我有一个应用程序它加载了一堆从资源文件的工具栏的图形,使用 <$ C $的组合C> Image.FromStream 和的 Assembly.GetManifestResourceStream

I have an application which loads a bunch of toolbar graphics from resource files, using a combination of Image.FromStream and Assembly.GetManifestResourceStream.

我遇到的问题是虽然这作品罚款的Windows 7,Windows XP上的应用程序崩溃,如果与这些图像中的一个用户界面元素被禁用。在Windows 7中,图像以灰度渲染。在XP中,它因内存不足异常崩溃。

The problem I'm having is while this works fine on Windows 7, on Windows XP the application crashes if a user interface element linked to one of these images is disabled. On Windows 7, the image is rendered in grayscale. On XP, it crashes with an out of memory exception.

在hairpulling的负载我终于它追踪到图像的初始加载。作为理所当然的事,如果我创建实现任何物体的IDisposable ,它也破坏了同样的方法,我将它包装在using语句,例如:

After a load of hairpulling I have finally traced it to the initial loading of the image. As a matter of course, if I create any object implementing IDisposable that is also destroyed in the same method, I wrap it in a using statement, for example

using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
{
   image = Image.FromStream(resourceStream);
}

如果我删除using语句,这样该流不配置,则应用程序不再崩溃在XP。但我现在有一堆挂着关于孤儿流 - 将图像存储在指挥班和他们自己设置,当这些正确处理图像,但原流不是

If I remove the using statement so that the stream isn't disposed, then the application no longer crashes on XP. But I now have a bunch of "orphan" streams hanging about - the images are stored in command classes and these correctly dispose of the images when they themselves are disposed, but the original stream isn't.

我检查的 FromStream 并确认流需要保持开放。为什么这并没有坠毁并烧毁了Windows 7的开发系统是一个谜但是!

I checked the documentation for FromStream and it confirms the stream needs to remain open. Why this hasn't crashed and burned on the Windows 7 development system is a mystery however!

我真的不希望这种流游逛,我一定不要不想有存储这个流的引用,以及图像,这样我可以在以后处置。我只有需要该流一次,所以我想摆脱它:)

I really don't want this stream hanging around, and I certainly don't want to have to store a reference to this stream as well as the image so I can dispose of it later. I only have need of that stream once so I want to get rid of it :)

是否有可能创造的形象,然后杀掉流的那里,然后?

Is it possible to create the image and then kill of the stream there and then?

推荐答案

流需要开放的原因是的以下

The reason the stream needs to be open is the following:

GDI +,因此在 System.Drawing中的命名空间,直到位由图像所需可推迟原始图像的比特的解码。此外,该图像已经被解码,即使之后,GDI +可以确定它是更有效地丢弃所述存储器为一个大的位图和以后重新解码。因此,GDI +必须具有访问源位的图像在位图图片的寿命对象。

GDI+, and therefore the System.Drawing namespace, may defer the decoding of raw image bits until the bits are required by the image. Additionally, even after the image has been decoded, GDI+ may determine that it is more efficient to discard the memory for a large Bitmap and to re-decode later. Therefore, GDI+ must have access to the source bits for the image for the life of the Bitmap or the Image object.

该文件的解决方法是使用创建一个或者非索引图像 Graphics.DrawImage 或者创建一个索引位图从这里描述的原始图像:

The documented workaround is to create either a non-indexed image using Graphics.DrawImage or to create an indexed Bitmap from the original image as described here:

位图和图像的构造函数依赖

Bitmap and Image constructor dependencies

这篇关于加载从流的图像不保持流开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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