WPF BitmapImage序列化/反序列化 [英] WPF BitmapImage Serialization/Deserialization

查看:129
本文介绍了WPF BitmapImage序列化/反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试序列化和反序列化BitmapImages。我一直在使用在此线程中发现的应该起作用的方法:我的byte []转换为WPF BitmapImage时出错了吗?

I've been trying to Serialize and Deserialize BitmapImages. I've been using methods which supposedly works which I found in this thread: error in my byte[] to WPF BitmapImage conversion?

只是要迭代正在发生的事情,这是我的序列化代码的一部分:

Just to iterate what is going on, here is part of my Serialization code:

using (MemoryStream ms = new MemoryStream())
                {
                    // This is a BitmapImage fetched from a dictionary.
                    BitmapImage image = kvp.Value; 

                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(image));
                    encoder.Save(ms);

                    byte[] buffer = ms.GetBuffer();

                    // Here I'm adding the byte[] array to SerializationInfo
                    info.AddValue((int)kvp.Key + "", buffer);
                }

这是反序列化代码:

// While iterating over SerializationInfo in the deserialization
// constructor I pull the byte[] array out of an 
// SerializationEntry
using (MemoryStream ms = new MemoryStream(entry.Value as byte[]))
                    {
                        ms.Position = 0;

                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        image.StreamSource = ms;
                        image.EndInit();

                        // Adding the timeframe-key and image back into the dictionary
                        CapturedTrades.Add(timeframe, image);
                    }

此外,我不确定这是否重要,但在我填充之前字典我使用PngBitmapEncoder对位图进行编码,以将其导入BitmapImages。因此不确定双编码是否与此有关。这是执行此操作的方法:

Also, I'm not sure if it matters but earlier when I populated my dictionary I encoded Bitmaps with PngBitmapEncoder to get them into BitmapImages. So not sure if double-encoding has something to do with it. Here's the method that does that:

// Just to clarify this is done before the BitmapImages are added to the
// dictionary that they are stored in above.
private BitmapImage BitmapConverter(Bitmap image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                BitmapImage bImg = new BitmapImage();
                bImg.BeginInit();
                bImg.StreamSource = new MemoryStream(ms.ToArray());
                bImg.EndInit();
                ms.Close();

                return bImg;
            }
        }

所以问题是,序列化和反序列化工作正常。没有错误,字典中的条目看起来像是BitmapImages,但是当我在调试模式下查看它们时,它们的宽度/高度和
其他一些属性都设置为 0。当然,当我尝试显示图像时什么也没显示。

So the problem is, serialization and deserialization works fine. No errors, and the dictionary has entries with what seems to be BitmapImages, however their width/height and some other properties are all set to '0' when I look at them in debugging-mode. And of course, nothing is shown when I try to display the images.

那么关于为什么未正确反序列化它们的任何想法吗?

So any ideas as to why they aren't properly deserialized?

谢谢!

推荐答案

1)您不应丢弃用于图像初始化的MemoryStream。在此行中删除使用

1) You should not dispose MemoryStream, used from image initializing. Remove using in this line

using (MemoryStream ms = new MemoryStream(entry.Value as byte[]))

2)

encoder.Save(ms);

尝试添加

ms.Seek(SeekOrigin.Begin, 0);
ms.ToArray();

这篇关于WPF BitmapImage序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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