开发者

WPF Write/Read BitmapImage to XML failing with 'No imaging component.....' Error

开发者 https://www.devze.com 2023-03-27 07:40 出处:网络
I am trying to write/read a BitmapImage to Xml using the XmlReader/XmlWriter classes. On writing out I can see a nice long CDATA section in the output Xml file. When reading in I can see that it is in

I am trying to write/read a BitmapImage to Xml using the XmlReader/XmlWriter classes. On writing out I can see a nice long CDATA section in the output Xml file. When reading in I can see that it is indeed reading in that same CDATA section data. But the attempt to recreate the BitmapImage is failing with the error...

"No imaging component suitable to complete this operation was found."

...on the line that says 'image.StreamSource = stream' below...

Public Sub WriteXmlImage(ByVal writer As XmlWriter, ByVal image as BitmapImage)
  开发者_JS百科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

In testing I use the following trivial code to create a test BitmapImage...

Dim image As New BitmapImage(New Uri("c:\devil.png"))

Any ideas?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号