Returing由Image.FromStream(流流)方法创建的映像 [英] Returing image created by Image.FromStream(Stream stream) Method
问题描述
我有这个函数返回函数内的图像的图像是使用 Image.FromStream 方式
创建根据的 MSDN :
您必须保持流打开图像
的寿命块引用>
所以我不关闭流(如果我不要关闭蒸汽的GDI +异常是从返回的图像对象抛出)。我的问题是流是否会关闭/处置在 Image.Dispose()被称为别的地方上返回的图像
公共静态图像的getImage(字节[]缓冲区,诠释抵消,诠释计数)
{
变种的MemoryStream =新的MemoryStream(缓冲区,偏移,计数) ;
返回Image.FromStream(MemoryStream的);
}
所建议的其中一个答案,是是不是要走的路,因为它会抛出一个异常:
公共静态图像的getImage(字节[]缓冲区,诠释抵消,使用诠释计数)
{
(VAR的MemoryStream =新的MemoryStream(缓冲区,偏移,计数))
{
返回Image.FromStream(MemoryStream的);
}
}
公共静态无效的主要()
{
VAR图像=的getImage(参数);
image.Save(路径); < - 抛出的异常
}
- 据一些人们明确的处理/关闭 的MemoryStream 是没有必要的,因为它不使用任何非托管资源,别人都说相反的事情,所以它的样的困境。
- Image.Dispose 方式不配置即自其创建映像流
- Image类没有按' T下保持到流的任何引用传递为 Image.FromStream 方法,因此流将最终被收集在 GC ...?因此,在异常的 Image.Save 方式
- 返回包含到流并通过它,因此使我们的创建图像引用的包装类处理他们两个......?或者干脆使用标签属性的引用保持父流...?
- 这个问题似乎只发生使用的MemoryStream <当/ STRONG>。如果图像是从 ConnectStream 虚无中创造的坏即使父流布置情况。
解决方案不管别人的意见做,你不应该关闭或处分流,直到图像布置。
的 MSDN 状态:
您必须保持流打开图像的寿命
块引用>
对于一些流,如
的MemoryStream
,配置没有多大用处,因为它不分配非托管资源。在另一方面文件流做,所以除非你非常确信流是安全的,你应该总是处置,当你与图像所做的流。I have this function which returns an Image within the function the image is created using the Image.FromStream method According to MSDN:
You must keep the stream open for the lifetime of the Image
So I'm not closing the stream(if I do close the steam a GDI+ exception is thrown from the returned image object). My question is whether the stream will be closed/disposed when Image.Dispose() is called somewhere else on the returned Image
public static Image GetImage(byte[] buffer, int offset, int count) { var memoryStream = new MemoryStream(buffer, offset, count); return Image.FromStream(memoryStream); }
As suggested in one of the answers, using is not the way to go, since it throws an exception:
public static Image GetImage(byte[] buffer, int offset, int count) { using(var memoryStream = new MemoryStream(buffer, offset, count)) { return Image.FromStream(memoryStream); } } public static void Main() { var image = GetImage(args); image.Save(path); <-- Throws exception }
- According to some people explicitly disposing/closing a MemoryStream is not necessary as it doesn't use any unmanaged resources others say the opposite thing so its kind of a dilemma.
- Image.Dispose method doesn't dispose the stream ftom which the Image was created
- The Image class doesn't hold any reference to the Stream passed to Image.FromStream method so the stream will eventually be collected by the GC...? Hence the exception in Image.Save method
- Return a wrapper class which contains a reference to the stream and the Image created by it hence enabling us to dispose both of them...? or simply use the Tag property to keep a reference to the parent stream...?
- This problem only seems to happen when using the MemoryStream. If the image is created from ConnectStream nothing bad happens even if the parent stream is disposed.
解决方案Despite what others advice to do, you should not close or dispose the stream until the image is disposed.
MSDN states:
You must keep the stream open for the lifetime of the Image.
For some streams, like
MemoryStream
, disposing doesn't have much use since it doesn't allocate unmanaged resources. File streams on the other hand do, so unless you are very sure the stream is safe, you should always dispose the stream when you are done with the image.这篇关于Returing由Image.FromStream(流流)方法创建的映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!