我正在尝试使用XmlReader / XmlWriter类将BitmapImage写入/读取到Xml。在写出时,我可以在输出Xml文件中看到一个很好的长CDATA部分。在读入时我可以看到它确实在同一CDATA部分数据中读取。但重新创建BitmapImage的尝试失败了,错误...
"No imaging component suitable to complete this operation was found."
...在下面显示'image.StreamSource = stream'的行上......
Public Sub WriteXmlImage(ByVal writer As XmlWriter, ByVal image as BitmapImage)
Using stream As New MemoryStream
Dim encoder = New PngBitmapEncoder
encoder.Frames.Add(BitmapFrame.Create(image))
encoder.Save(stream)
writer.WriteCData(Convert.ToBase64String(stream.ToArray()))
End Using
End Sub
Public Function ReadXmlImage(ByVal reader As XmlReader) As BitmapImage
Using stream As New IO.MemoryStream(Convert.FromBase64String(reader.Value))
Dim image As New BitmapImage
image.BeginInit()
image.StreamSource = stream
image.EndInit()
return image
End Using
End Sub
在测试中,我使用以下简单的代码来创建测试BitmapImage ...
Dim image As New BitmapImage(New Uri("c:\devil.png"))
有什么想法吗?