我正在尝试了解图像序列化在WPF中的工作原理。我有以下课程:
[Serializable]
public class TestClass : ISerializable
{
public TestClass() { }
protected TestClass(SerializationInfo info, StreamingContext context)
{
SerializedImage = (byte[])info.GetValue("SerializedImage", typeof(byte[]));
}
public byte[] SerializedImage { get; set; }
public Image Image { get; set; }
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("SerializedImage", SerializedImage);
}
[OnSerializing]
private void OnSerializing(StreamingContext sc)
{
BitmapImage image = Image.Source as BitmapImage;
MemoryStream stream = new MemoryStream();
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
SerializedImage = stream.ToArray();
stream.Close(); ;
}
[OnDeserialized]
private void OnDeserialized(StreamingContext sc)
{
MemoryStream stream = new MemoryStream(SerializedImage);
Image = new Image
{
Source = BitmapFrame.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad)
};
stream.Close();
}
}
这是Xaml代码:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="358" Width="300" ResizeMode="NoResize">
<StackPanel>
<Image x:Name="Screenshot" Height="178" />
<Button Width="80" Height="30" Content="Load Image" Click="Button_Click_1" />
<Button Width="92" Height="30" Content="Save to file" Click="Button_Click_2" />
<Button Width="92" Height="30" Content="Load File" Click="Button_Click_3" />
<Button Width="92" Height="30" Content="Load File (1)" Click="Button_Click_4" />
</StackPanel>
这就是背后的代码:
public partial class Window1 : Window
{
TestClass test;
public Window1()
{
InitializeComponent();
test = new TestClass();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog { Filter = "Bmp Image | *.bmp" };
dialog.ShowDialog();
if (dialog.FileName != string.Empty)
{
Screenshot.Source = new BitmapImage(new Uri(dialog.FileName));
test.Image = Screenshot;
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog { DefaultExt = ".t", AddExtension = true };
dialog.ShowDialog();
if (dialog.FileName != string.Empty)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(dialog.FileName, FileMode.Create);
formatter.Serialize(stream, test);
stream.Close();
}
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog { Filter = "TEST file | *.t" };
dialog.ShowDialog();
if (dialog.FileName != string.Empty)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(dialog.FileName, FileMode.Open);
test = formatter.Deserialize(stream) as TestClass;
stream.Close();
Screenshot = test.Image;
}
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog { Filter = "TEST file | *.t" };
dialog.ShowDialog();
if (dialog.FileName != string.Empty)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(dialog.FileName, FileMode.Open);
test = formatter.Deserialize(stream) as TestClass;
stream.Close();
Screenshot.Source = null;
MemoryStream stream1 = new MemoryStream(test.SerializedImage);
Screenshot.Source = BitmapFrame.Create(stream1, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
}
}
}
现在由于某些我不知道的原因,TestClass中图像的反序列化效果不佳。如果我抓住字节数组并将其自身转换回图像(Button_Click_4),它就可以工作,图像会显示在我的表单上。相反,如果我直接从TestClass中的Image属性获取图像,则表单上不会显示任何内容。我不知道这是怎么可能的,因为两种情况下涉及的代码是相同的,除非在幕后发生其他事情。
我做错了什么?我提供了完整的代码,您可以将其粘贴并运行以查看问题。
答案 0 :(得分:1)
可视树中的Image对象由名为“Screenshot”的字段引用。将此.creenshot分配给新图像根本不会改变您的可视树。它只是分配字段截图。如果您设置了Source,则表示您正在更新可视树中的Image。
理想情况下,您的测试类应该公开ImageSource属性,而不是Image。单个Image实例只能在可视树中出现一次。因此,通过公开ImageSource属性,您可以在Image的多个实例中重用它(可以在可视树中的不同位置使用)。