L
Larry Serflaten
Acording to Bob Powell, serializing an object should be a breeze:
http://groups.google.com/groups?hl=en&lr=&safe=off&selm=#TR3qvCcCHA.2544@tkmsftngp11
But its not happening, and I can't see why not. When I save the
file, it does not have near enough data to contain the object,
so, no way will I be able to deserialize it.
Can anyone see where I went wrong? No errors are reported, but I see no
image being saved, or loaded in. The other types are saved, and loaded in
without errors. Why isn't the Image field working?
Here is my serialization code, followed by some Form code
showing how I am using it:
<Serializable()> _
Public Structure Settings
Public Background As Image
Public Counter As Integer
Public Location As Point
Public Size As Size
End Structure
Public Class SettingsIO
Public Shared Function FromBinaryFile(ByVal FileName As String) As Settings
' Read settings file
Dim data As Stream
Try
data = File.Open(FileName, FileMode.Open, FileAccess.Read)
Dim bf As New BinaryFormatter
FromBinaryFile = CType(bf.Deserialize(data), Settings)
Catch ex1 As FileNotFoundException
' First time through, file is not present
FromBinaryFile = New Settings
Catch ex2 As Exception
' Anything else wrong, inform user
MsgBox(ex2.Message, MsgBoxStyle.OKOnly, "Initialization")
FromBinaryFile = New Settings
Finally
If Not data Is Nothing Then data.Close()
End Try
End Function
Public Shared Sub ToBinaryFile(ByVal FileName As String, ByVal AppSettings As Settings)
' Write settings file
Dim data As Stream
Try
data = File.Open(FileName, FileMode.Create, FileAccess.ReadWrite)
Dim bin As BinaryFormatter = New BinaryFormatter
bin.Serialize(data, AppSettings)
Catch ex As Exception
MsgBox(ex.Message)
Finally
If Not data Is Nothing Then data.Close()
End Try
End Sub
End Class
Usage:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mySettings = SettingsIO.FromBinaryFile(DataFile)
Me.BackgroundImage = mySettings.Background
Me.Location = mySettings.Location
Me.Size = mySettings.Size
Me.Text = String.Format("Accessed {0} times previously.", MySettings.Counter)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
MySettings.Background = Me.BackgroundImage
MySettings.Counter += 1
MySettings.Location = Me.Location
MySettings.Size = Me.Size
SettingsIO.ToBinaryFile(DataFile, MySettings)
End Sub
Again, the Location, Size and Counter do get saved, and read back in,
but the image does not.... Why not?
LFS
http://groups.google.com/groups?hl=en&lr=&safe=off&selm=#TR3qvCcCHA.2544@tkmsftngp11
But its not happening, and I can't see why not. When I save the
file, it does not have near enough data to contain the object,
so, no way will I be able to deserialize it.
Can anyone see where I went wrong? No errors are reported, but I see no
image being saved, or loaded in. The other types are saved, and loaded in
without errors. Why isn't the Image field working?
Here is my serialization code, followed by some Form code
showing how I am using it:
<Serializable()> _
Public Structure Settings
Public Background As Image
Public Counter As Integer
Public Location As Point
Public Size As Size
End Structure
Public Class SettingsIO
Public Shared Function FromBinaryFile(ByVal FileName As String) As Settings
' Read settings file
Dim data As Stream
Try
data = File.Open(FileName, FileMode.Open, FileAccess.Read)
Dim bf As New BinaryFormatter
FromBinaryFile = CType(bf.Deserialize(data), Settings)
Catch ex1 As FileNotFoundException
' First time through, file is not present
FromBinaryFile = New Settings
Catch ex2 As Exception
' Anything else wrong, inform user
MsgBox(ex2.Message, MsgBoxStyle.OKOnly, "Initialization")
FromBinaryFile = New Settings
Finally
If Not data Is Nothing Then data.Close()
End Try
End Function
Public Shared Sub ToBinaryFile(ByVal FileName As String, ByVal AppSettings As Settings)
' Write settings file
Dim data As Stream
Try
data = File.Open(FileName, FileMode.Create, FileAccess.ReadWrite)
Dim bin As BinaryFormatter = New BinaryFormatter
bin.Serialize(data, AppSettings)
Catch ex As Exception
MsgBox(ex.Message)
Finally
If Not data Is Nothing Then data.Close()
End Try
End Sub
End Class
Usage:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mySettings = SettingsIO.FromBinaryFile(DataFile)
Me.BackgroundImage = mySettings.Background
Me.Location = mySettings.Location
Me.Size = mySettings.Size
Me.Text = String.Format("Accessed {0} times previously.", MySettings.Counter)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
MySettings.Background = Me.BackgroundImage
MySettings.Counter += 1
MySettings.Location = Me.Location
MySettings.Size = Me.Size
SettingsIO.ToBinaryFile(DataFile, MySettings)
End Sub
Again, the Location, Size and Counter do get saved, and read back in,
but the image does not.... Why not?
LFS