c# – 无法访问已关闭的流
发布时间:2020-12-15 04:13:42 所属栏目:百科 来源:网络整理
导读:我试图使用 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 t
我试图使用
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 } 但在加载过程中,我在“新的PngBitmapDecoder”行中得到以下异常:
我明白我在上面的代码中关闭了流,但是在它退出之前是不是通过_cache.Add()创建一个副本(通过Serialization)?串流化流的正确过程是什么? 谢谢! 解决方法
不必要.如果它是“在进行中”,它将只存储对象引用;流不是真的非常可序列化(一个流是一个软管,而不是一个桶). 你想存储BLOB – 不是流: _cache.Add(someId,stream.ToArray()); ... byte[] blob = _cache.GetData(someId); if(blob != null) { using(Stream inStream = new MemoryStream(blob)) { // (read) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |