473,395 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Serialization is not working

Acording to Bob Powell, serializing an object should be a breeze:

http://groups.google.com/groups?hl=e...%40tkmsftngp11

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
Nov 21 '05 #1
6 1563
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

------------------------

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
Acording to Bob Powell, serializing an object should be a breeze:

http://groups.google.com/groups?hl=e...tkmsftngp11But its not happening, and I can't see why not. When I save thefile, 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 noimage being saved, or loaded in. The other types are saved, and loaded inwithout errors. Why isn't the Image field working?Here is my serialization code, followed by some Form codeshowing how I am using it:<Serializable()> _Public Structure Settings Public Background As Image Public Counter As Integer Public Location As Point Public Size As SizeEnd StructurePublic Class SettingsIO Public Shared Function FromBinaryFile(ByVal FileName As String) AsSettings ' 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, ByValAppSettings 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 SubEnd ClassUsage: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e AsSystem.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 AsSystem.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 SubAgain, the Location, Size and Counter do get saved, and read back in,but the image does not.... Why not?LFS

Nov 21 '05 #2
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
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
Acording to Bob Powell, serializing an object should be a breeze:

http://groups.google.com/groups?hl=e...%40tkmsftngp11

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

Nov 21 '05 #3
Hi,

Converts data to bytes to be saved or transmitted to another
location.
http://msdn.microsoft.com/library/de...ialization.asp

Ken
---------------------------
"OpticTygre" <op********@adelphia.net> wrote in message
news:TM********************@adelphia.com...
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
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
Acording to Bob Powell, serializing an object should be a breeze:

http://groups.google.com/groups?hl=e...%40tkmsftngp11

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


Nov 21 '05 #4
In addition to Ken's comments, here's a very nice 3-part article on
Serialization:

Part 1: http://msdn.microsoft.com/msdnmag/issues/02/04/net/
Part 2: http://msdn.microsoft.com/msdnmag/issues/02/07/net/
Part 3: http://msdn.microsoft.com/msdnmag/issues/02/09/net/

hope that helps..
Imran.

"OpticTygre" <op********@adelphia.net> wrote in message
news:TM********************@adelphia.com...
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
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:O$**************@TK2MSFTNGP11.phx.gbl...
Acording to Bob Powell, serializing an object should be a breeze:

http://groups.google.com/groups?hl=e...%40tkmsftngp11
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


Nov 21 '05 #5

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote

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
Nov 21 '05 #6

"Larry Serflaten" <se*******@usinternet.com> wrote
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

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Veeneman | last post by:
I'm working on a project where I have to persist data to a file, rather than to a database. Basically, I need to save the state of several classes, each of which will have a couple of dozen...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
2
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
5
by: Tamir Khason | last post by:
Hi, all Two classes Foo1 and Foo2 Foo1 uses Foo2 as reference Both are strong name signed with the same key pair I'm performing Binary Serialization of object inside Foo2 from Foo1 as following:...
8
by: vinay | last post by:
Hi Guys I want to understand Serialization. What is serialization. When do we need to use?? What are advantages and Disadvantages. Also please diret me to some good sites on serialization....
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
0
by: Michael | last post by:
I have a problem with serialization in my project. Serialization is working fine so far but when I increase the version of the application in the AssemblyInfo.cs I get an exception. So I created a...
0
by: elziko | last post by:
In the past when doing binary serialization I have been able to implement my own serialization for a type by defining a serialization surrogate for this type. I am now working on something...
5
by: RobinS | last post by:
I want to serialize a class that I am using to retain some information the user types into a screen. I have 3 questions. 1) I serialized it as XML to start with. This works, but how do I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.