问题描述
我有这个函数,它在使用 Image.FromStream 方法创建图像的函数中返回一个图像根据 MSDN:
您必须在图像的生命周期内保持流打开
所以我不会关闭流(如果我确实关闭了流,则从返回的图像对象中抛出 GDI+ 异常).我的问题是当 Image.Dispose() 在返回的图像上的其他地方被调用时,流是否会关闭/处置
public static Image GetImage(byte[] buffer, int offset, int count){var memoryStream = new MemoryStream(buffer, offset, count);返回 Image.FromStream(memoryStream);}
正如其中一个答案所建议的那样,使用不是可行的方法,因为它会引发异常:
public static Image GetImage(byte[] buffer, int offset, int count){使用(var memoryStream = new MemoryStream(缓冲区,偏移量,计数)){返回 Image.FromStream(memoryStream);}}public static void Main(){var image = GetImage(args);图像.保存(路径);<-- 抛出异常}
- 根据某些人明确的说法处置/关闭 MemoryStream 不是必需的,因为它不使用任何非托管资源 其他人说相反的事情所以这是一种进退两难.
- Image.Dispose 方法不会处理创建 Image 的流
- Image 类不持有对传递给 Image.FromStream 方法的 Stream 的任何引用,因此该流最终将被 GC 收集...?因此 Image.Save 方法中的异常
- 返回一个包装类,其中包含对流的引用和由它创建的图像,从而使我们能够处理这两者......?或者只是使用 Tag 属性来保持对父流的引用......?
- 这个问题似乎只在使用 MemoryStream 时发生.如果图像是从 ConnectStream 创建的,即使父流被释放,也不会发生坏.
尽管其他人建议这样做,但在处理图像之前,您不应关闭或处理流.
MSDN 指出:>
您必须在图像的整个生命周期内保持流打开.
对于某些流,例如 MemoryStream
,处置没有太大用处,因为它不分配非托管资源.另一方面,文件流会这样做,因此除非您非常确定流是安全的,否则您应该始终在处理完图像后处理流.
I have this function which returns an Image within the function the image is created using the Image.FromStream methodAccording to MSDN:
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:
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.
这篇关于返回由 Image.FromStream(Stream stream) 方法创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!