返回由Image.FromStream(Stream stream)方法创建的图像 [英] Returning image created by Image.FromStream(Stream stream) Method

查看:1968
本文介绍了返回由Image.FromStream(Stream stream)方法创建的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数在函数中返回一个Image,使用 Image.FromStream 方法创建图像
根据 MSDN

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

所以我没有关闭流(如果我关闭了蒸汽,从返回的图像对象抛出GDI +异常)。我的问题是,当在返回的图片上的其他位置调用 Image.Dispose()时,流是否会关闭/处置

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
}




  1. 根据一些人们明确地处理/关闭 MemoryStream 是没有必要的,因为它不使用任何非托管资源,其他人说相反的事情,所以它是一种两难的选择。

  2. Image.Dispose 方法不会处理创建Image的流ftom

  3. Image类没有' t保持传递给 Image.FromStream 方法的Stream的任何引用,以便最终由 GC ...收集流?因此 Image.Save 方法中的例外

  4. 返回一个包装类,其中包含对流的引用和由它创建的Image,因此使我们能够处置他们两个......?或者只是使用标记属性来保持对父流的引用...?

  5. 当使用 MemoryStream <时,似乎只会发生此问题/ strong>即可。如果图像是从 ConnectStream 创建的,即使父流已被处理,也不会发生错误

  1. 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.
  2. Image.Dispose method doesn't dispose the stream ftom which the Image was created
  3. 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
  4. 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...?
  5. 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 声明:


您必须在图片的生命周期内保持流打开。

You must keep the stream open for the lifetime of the Image.

对于某些流,例如 MemoryStream ,处理没有多大用处它不分配非托管资源。另一方面,文件流也是如此,因此,除非您非常确定流是安全的,否则在完成图像后应始终处理流。

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)方法创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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