Image.FromStream不保存对基础流的引用 [英] Image.FromStream does not hold a ref to the underlying stream

查看:143
本文介绍了Image.FromStream不保存对基础流的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些可以的代码

MemoryStream ms = new MemoryStream();
...
return Image.FromStream(ms);

由于Image对象不保留对流的引用,因此它以非常折衷的方式失败,因此,如果GC踢出该对象会导致GDI +错误,则该对象可以被处置.

It fails in very eclectic ways since the Image object does not hold a ref to the stream, so it can get disposed if the GC kicks in which results in GDI+ errors.

如何解决此问题(不将流保存到磁盘或更改方法信号)?

How do I work around this (without saving the stream to disk, or altering my method sigs) ?

推荐答案

在我看来这不太可能-几乎所有Image.FromStream的使用都会引起问题.

This seems highly unlikely to me - it would cause a problem for almost any use of Image.FromStream.

对我来说,似乎更有可能处置了您的MemoryStream,这是不应该的.

It seems more likely to me that something's disposing of your MemoryStream, which it shouldn't.

您能提供一个简短但完整的程序来演示该问题吗?强制垃圾回收应该使复制相对容易-您甚至可以使用终结器创建从MemoryStream派生的自己的类,以显示是否确实收集了 (嗯,至少已完成)

Could you provide a short but complete program which demonstrates the problem? Forcing garbage collection should make it relatively easy to reproduce - you could even create your own class deriving from MemoryStream with a finalizer to show whether or not it really is being collected (well, finalized at least).

这篇关于Image.FromStream不保存对基础流的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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