返回由 Image.FromStream(Stream stream) 方法创建的图像

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

问题描述

我有这个函数,它在使用 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);图像.保存(路径);<-- 抛出异常}

  1. 根据某些人明确的说法处置/关闭 MemoryStream 不是必需的,因为它不使用任何非托管资源 其他人说相反的事情所以这是一种进退两难.
  2. Image.Dispose 方法不会处理创建 Image 的流
  3. Image 类不持有对传递给 Image.FromStream 方法的 Stream 的任何引用,因此该流最终将被 GC 收集...?因此 Image.Save 方法中的异常
  4. 返回一个包装类,其中包含对流的引用和由它创建的图像,从而使我们能够处理这两者......?或者只是使用 Tag 属性来保持对父流的引用......?
  5. 这个问题似乎只在使用 MemoryStream 时发生.如果图像是从 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
}

  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 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.

这篇关于返回由 Image.FromStream(Stream stream) 方法创建的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)
Why do I get quot;error: ... must be a reference typequot; in my C# generic method?(为什么我会收到“错误:...必须是引用类型?在我的 C# 泛型方法中?)
Strange LINQ Exception (Index out of bounds)(奇怪的 LINQ 异常(索引越界))