Reputation: 467
I have created a custom imagelist class in VB.NET, and I would like to serialize / deserialize it.
Imports System.Xml.Serialization
<Serializable()>
Public Class clsImageList
Public Images As New List(Of clsImageItem)
Public Sub Add(ByVal uGUID As String, ByRef uImage As Image)
Dim nItem As New clsImageItem(uImage, uGUID)
Images.Add(nItem)
End Sub
Public Function Image(ByVal uIndex As Integer) As Image
Return Images.Item(uIndex).Image
End Function
End Class
Public Class clsImageItem
Public ReadOnly Property [Image] As Image
Private _sGUID As String
Public Sub New(uImage As Image, uGUID As String)
Image = uImage
_sGUID = uGUID
End Sub
End Class
Using a regular ImageList, I did it like that:
Public Function ImageListToBytes(ByRef uImageList As ImageList) As Byte()
Try
Using ms As New MemoryStream
Dim bf As New BinaryFormatter()
bf.Serialize(ms, uImageList.ImageStream)
Return ms.ToArray
End Using
Catch ex As Exception
Debug.Assert(False)
Return Nothing
End Try
End Function
Public Sub BytesToImageList(ByRef uBytes() As Byte, ByRef uImageList As ImageList)
Using ms As New MemoryStream()
ms.Write(uBytes, 0, uBytes.Length)
ms.Seek(0, SeekOrigin.Begin)
Dim bf As New BinaryFormatter
uImageList.ImageStream = DirectCast(bf.Deserialize(ms), ImageListStreamer)
End Using
End Sub
Can somebody tell me how I can serialize / deserialize my clsImageList?
Upvotes: 0
Views: 67
Reputation: 125197
The first thing you should do is marking your clsImageItem
also Serializable
.
Then try serialize and deserialize clsImageList
class. so in your serialization and deserialization methods, pass an instance of clsImageList
and serialize and deserialize it.
Public Function ImageListToBytes(ByRef c As clsImageList) As Byte()
Try
Using ms As New MemoryStream
Dim bf As New BinaryFormatter()
bf.Serialize(ms, c)
Return ms.ToArray
End Using
Catch ex As Exception
Debug.Assert(False)
Return Nothing
End Try
End Function
Public Sub BytesToImageList(ByRef uBytes() As Byte, ByRef c As clsImageList)
Using ms As New MemoryStream()
ms.Write(uBytes, 0, uBytes.Length)
ms.Seek(0, SeekOrigin.Begin)
Dim bf As New BinaryFormatter
c = DirectCast(bf.Deserialize(ms), clsImageList)
End Using
End Sub
Upvotes: 2