Caching Application Blockを使用していくつかの画像をキャッシュしようとしています (これらの画像はレンダリングに時間がかかります)
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() が (シリアライゼーションを介して) コピーを作成していませんか? ストリームをシリアル化する正しいプロセスは何ですか?
ありがとう!