Serialize/Deserialize Font to/from string

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

How do I serialize Font object into a string that I can store in either
INI file or the registry (I know it’s a bad thing, but need to do it
nevertheless). Then, also, how do I deserialize it from that string
back into a font object?

Thanks.
 
Frank said:
How do I serialize Font object into a string that I can store in
either INI file or the registry (I know it's a bad thing, but need to
do it nevertheless). Then, also, how do I deserialize it from that
string back into a font object?

Look into xml serialisation and the XmlSerializer class.
 
Sven said:
Look into xml serialisation and the XmlSerializer class.

Doesn't see to work. Following line returns an error saying that since
Font object does not have a default constructor, it can't do anything
with it.

Dim oXml As New Xml.Serialization.XmlSerializer(GetType(Font))
 
How do I serialize Font object into a string that I can store in either
INI file or the registry (I know it¢s a bad thing, but need to do it
nevertheless). Then, also, how do I deserialize it from that string
back into a font object?

Thanks.

Frank...

Here is a little bit of code using a binary formatter to convert a font
object into a base-64 string:

Option Strict On
Option Explict On

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

.....
' Basic error handling - you'll want to improve :)
Private Function SerializeFontObject(ByVal fnt As Font) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream

Try
bf.Serialize(mem, fnt)
Return Convert.ToBase64String(mem.ToArray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

' again, very basic handling..
Private Function DeserializeFontObject(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Convert.FromBase64String(fnt))

Try
Return DirectCast(bf.Deserialize(mem), Font)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try
End Function

HTH
 
Hi Frank,

You cannot serialize all objects, as far as I know is this feature only for
collection objects which depends on certain rules.

I never saw it done for control objects.

In my opinion can this the simplest be done for the font with setting the
properties from that object to string.

Cor
 
Hi Tom,

I was mixed up with XML serializing, however thanks for the sample, I did
not know this one.

Cor
 
Btw, this sample will be awesome in VS2005 with generics

class SerializeAnyFreakingObject (Of T)
'as long it supports serialization, of course

Private Function SerializeFontObject(ByVal oObject As T) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream

Try
bf.Serialize(mem, oObject)
Return Convert.ToBase64String(mem.ToArray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

Private Function DeserializeFontObject(ByVal sString As String) As T
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Convert.FromBase64String(sString))

Try
Return DirectCast(bf.Deserialize(mem), T)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try

End Function
End class
 
Btw, this sample will be awesome in VS2005 with generics

class SerializeAnyFreakingObject (Of T)
'as long it supports serialization, of course

Private Function SerializeFontObject(ByVal oObject As T) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream

Try
bf.Serialize(mem, oObject)
Return Convert.ToBase64String(mem.ToArray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

Private Function DeserializeFontObject(ByVal sString As String) As T
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Convert.FromBase64String(sString))

Try
Return DirectCast(bf.Deserialize(mem), T)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try

End Function
End class

Generics will be nice... But, I think it should work now if you change
the type to object (and again, as long as the object supports
serilization :)
 
Back
Top