Cannot Access Closed Stream

后端 未结 2 2022
失恋的感觉
失恋的感觉 2021-01-17 17:45

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 s         


        
相关标签:
2条回答
  • 2021-01-17 17:56

    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.

    0 讨论(0)
  • 2021-01-17 18:02

    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)
        } 
    }
    
    0 讨论(0)
提交回复
热议问题