3
我一直在尝试序列化和反序列化BitmapImages。我一直在使用方法据称工作,我在这个线程发现:error in my byte[] to WPF BitmapImage conversion?WPF BitmapImage序列化/反序列化
只是为了迭代是怎么回事,这里是我的序列化代码的一部分:
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。所以不确定双重编码是否与它有关。这是这样做的方法:
// 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'。当然,当我尝试显示图像时没有任何显示。
因此,有什么想法,为什么他们没有正确的反序列化?
谢谢!
再度回到今天我看到帮我,你DeflateStreams yeseterday如果你记得帮我。应用您的更改,现在它完美地工作,再次感谢! – vesz 2010-11-29 22:51:05