Serialization is not working

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
 
K

Ken Tucker [MVP]

Hi,

I had no problems serializing the structure and deserializing it.


This is how I saved the structure

Dim s As New Settings

With s

..Background = Image.FromFile("C:\camera.bmp")

..Counter = 10

..Location = New Point(10, 10)

..Size = New Size(32, 32)

End With

Dim fs As New FileStream("C:\Test.usr", FileMode.OpenOrCreate)

'Get a Binary Formatter instance

Dim bf As New BinaryFormatter

'Serialize s

bf.Serialize(fs, s)

fs.Close()



How to deserialize



Dim fs As New FileStream("C:\Test.usr", FileMode.Open)

'Get a Binary Formatter instance

Dim bf As New BinaryFormatter

'Deserialize c from strFilename2

'Note that the deserialized object must be cast to the proper type.

s = CType(bf.Deserialize(fs), Settings)

'Close the file and release resources (avoids GC delays)

fs.Close()

PictureBox1.Image = s.Background



Ken
 
O

OpticTygre

Just read this post, and I was curious...what is serialization? It's one
part of VB.NET that I have yet to be exposed to. What is it, how does it
work, what is an example of a real-world application that uses it, etc...
Are there any good articles out there on it?

-Jason
 
K

Ken Tucker [MVP]

Hi,

Converts data to bytes to be saved or transmitted to another
location.
http://msdn.microsoft.com/library/d...pref/html/frlrfsystemruntimeserialization.asp

Ken
---------------------------
Just read this post, and I was curious...what is serialization? It's one
part of VB.NET that I have yet to be exposed to. What is it, how does it
work, what is an example of a real-world application that uses it, etc...
Are there any good articles out there on it?

-Jason
 
L

Larry Serflaten

Ken Tucker said:
I had no problems serializing the structure and deserializing it.

I don't understand why I am have having trouble.
This is how I saved the structure


Dim fs As New FileStream("C:\Test.usr", FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
bf.Serialize(fs, s)
fs.Close()

This is how I did it:

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


How to deserialize

Dim fs As New FileStream("C:\Test.usr", FileMode.Open)
Dim bf As New BinaryFormatter
s = CType(bf.Deserialize(fs), Settings)
fs.Close()

This is how I did it

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



Aside from how we open files, it is essentially the same method.
As I said, the other members get saved and loaded correctly, and the
process runs without errors. It is just the image that does not get
handled correctly.

LFS
 
L

Larry Serflaten

Larry Serflaten said:
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.

I found the problem. I was using a relative file name, [ "data.dat" ]
and while it would work to load and save the settings, if that is all I
did, any time I used a dialog box (to load an image) the working path
got changed and the data was actually written somewhere else. It was
difficult to find because it would work sometimes, and other times not
depending on where the working directory was left pointing to...

Thanks to those who replied and helped me to see the error of my ways.

Live and learn!
LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top