0

I have made a small application to serialize and deserialize an image in xml. What I'm attempting to do is serialize then deserialize it as a bitmap type and return it. I then want to put this image into a listbox so I created a pictureBox pro-grammatically however when I try and cast the memory stream as a Bitmap I get the following error Additional information: Unable to translate bytes [FF] at index 0 from specified code page to Unicode which is a DescoderFallbackException. I'm unsure what GetType I should be giving the XMLSerializedr class but I assume it's Bitmap.

My code is as follows

Code for calling Serializer and Deserializer method and getting returned image to put in picturebox

 Dim mem As MemoryStream

            Serialize()
            Dim pic As Bitmap = Deserialize(mem)
            ListBoxTemplates.Items.Clear()
            Dim selectedCatID = AppSettings.PowerPoint.TemplateCategories.SingleOrDefault(Function(t) t.Name = ComboBoxNewLevel1.Text).ID
            Dim selectedCat2ID = AppSettings.PowerPoint.TemplateCategories.SingleOrDefault(Function(t) t.Name = ComboBoxNewLevel2.Text).ID


            Dim level3Cat = From Temp In AppSettings.PowerPoint.PSLTemplates
                            Join cat In AppSettings.PowerPoint.TemplateCategories On Temp.CategoryLevel1ID Equals cat.ID
                            Where Temp.CategoryLevel1ID = selectedCatID And selectedCat2ID = Temp.CategoryLevel2ID
                            Select Temp.Name

            For Each t In level3Cat

                Dim pb As PictureBox = New PictureBox()

                pb.Location = Location()

                pb.Name = ""
                pb.BackgroundImage = pic


                ListBoxTemplates.Items.Add(pb)
            Next

Serialize Method

Public Sub Serialize()

    Dim corolla As New Bitmap("C:\xmltext.png", True)


    Dim ms As New MemoryStream()

    corolla.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)

    Dim sw As New StringWriter()

    Dim xw As New XmlTextWriter(sw)

    xw.Formatting = Formatting.Indented

    Dim ser As New XmlSerializer(ms.GetBuffer().[GetType]())

    ser.Serialize(xw, ms.GetBuffer())

    Debug.WriteLine("Serialized Content::")

    Dim sSerializedContent As String = sw.GetStringBuilder().ToString()

    Debug.WriteLine(sSerializedContent)
    mem = ms
    xw.Close()

End Sub

Deserialize Method (where the problem lies)

Public Shared Function Deserialize(xmlStream As IO.MemoryStream) As Bitmap
    Dim serializer As New System.Xml.Serialization.XmlSerializer(GetType(Bitmap))
    xmlStream.Seek(0, IO.SeekOrigin.Begin)
    Return DirectCast(serializer.Deserialize(xmlStream), Bitmap)
End Function
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • [Use CDATA to store raw binary streams?](https://stackoverflow.com/q/502857/1115360) might give you ideas on how to do it. – Andrew Morton Nov 24 '17 at 10:55
  • You are serializing a JPEG. How do you expect this to be castable to a Bitmap object? You have to load the JPEG into a bitmap, e.g. with the appropriate constructor overload. – Nico Schertler Nov 24 '17 at 11:10

0 Answers0