无法访问关闭的流

发布于 2024-08-22 16:36:00 字数 1131 浏览 4 评论 0原文

我正在尝试使用 缓存应用程序块 来缓存一些图像(这些图像需要很长时间才能渲染)

  BitmapSource bitmapSource; ///some bitmap source already created
  _cache ///  Caching Application Block
  String someId; //id for this image, used as the key for the cache

  using (var stream = new MemoryStream())
    {
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Interlace = PngInterlaceOption.On;
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));             
        encoder.Save(stream);
        _cache.Add(someId, stream);
    }

然后使用以下方式加载它们:

imStream = (Stream)_cache.GetData(someId));
if (imStream != null)
{
    PngBitmapDecoder decoder = new PngBitmapDecoder(imStream,  BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    return decoder.Frames[0];  //return the bitmap source
}

但是在加载过程中,我在“new PngBitmapDecoder”行中收到以下异常:

“无法访问关闭的流。

我知道我在上面的代码中关闭了流,但是 _cache.Add() 不是在退出之前制作副本(通过序列化)吗?序列化流的正确过程是什么?

谢谢!

I'm trying to use the Caching Application Block to cache some images (these images take a long time to render)

  BitmapSource bitmapSource; ///some bitmap source already created
  _cache ///  Caching Application Block
  String someId; //id for this image, used as the key for the cache

  using (var stream = new MemoryStream())
    {
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Interlace = PngInterlaceOption.On;
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));             
        encoder.Save(stream);
        _cache.Add(someId, stream);
    }

And then load them using:

imStream = (Stream)_cache.GetData(someId));
if (imStream != null)
{
    PngBitmapDecoder decoder = new PngBitmapDecoder(imStream,  BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    return decoder.Frames[0];  //return the bitmap source
}

But during the load, i get the following exception at that "new PngBitmapDecoder" line:

"Cannot access a closed Stream.

I understand I closed the stream in the above code, but isn't _cache.Add() making a copy (via Serialization) before it exits? What's the correct process of serializing the stream?

THanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

维持三分热 2024-08-29 16:36:00

问题是流在 using 块末尾关闭(通过 Dispose())。您保留对关闭流的引用。

相反,将流的内容保存到缓存中:

_cache.Add(someId, stream.ToArray());

当您调用 PngBitmapDecoder 构造函数时,您必须创建一个新的 MemoryStream 来从该字节数组中读取。

The problem is that the stream is closed (via Dispose()) at the end of the using block. You retain a reference to the closed stream.

Instead, save the contents of the stream to your cache:

_cache.Add(someId, stream.ToArray());

When you call the PngBitmapDecoder constructor, you'll have to create a new MemoryStream to read from that byte array.

不再让梦枯萎 2024-08-29 16:36:00

但是 _cache.Add() 不是在退出之前创建一个副本(通过序列化)吗?

未必。如果它是“正在处理”,它将只存储对象引用;无论如何,Stream 并不是真正可序列化的(Stream 是软管,而不是桶)。

您想要存储 BLOB - 而不是 Stream

    _cache.Add(someId, stream.ToArray());

...

byte[] blob = _cache.GetData(someId);
if(blob != null) {
    using(Stream inStream = new MemoryStream(blob)) {
         // (read)
    } 
}

but isn't _cache.Add() making a copy (via Serialization) before it exits?

Not necessarily. If it is "in process" it will just store the object reference; Stream isn't really very serializable anyway (a Stream is a hose, not a bucket).

You want to store the BLOB - not the Stream:

    _cache.Add(someId, stream.ToArray());

...

byte[] blob = _cache.GetData(someId);
if(blob != null) {
    using(Stream inStream = new MemoryStream(blob)) {
         // (read)
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文