1

我正在尝试使用 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"))

有任何想法吗?

4

0 回答 0