By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,968 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,968 IT Pros & Developers. It's quick & easy.

Serialize/Deserialize Font to/from string

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Frank Rizzo wrote:
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 Groot

http://unforgiven.bloghorn.com
Nov 20 '05 #2

P: n/a
Sven Groot wrote:
Frank Rizzo wrote:
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.


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))

Nov 20 '05 #3

P: n/a
On Tue, 18 May 2004 14:34:44 -0700, Frank Rizzo wrote:
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
Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
Thanks, Tom.

This did the trick. The Convert.* was the missing link in my head.

Tom Shelton wrote:
On Tue, 18 May 2004 14:34:44 -0700, Frank Rizzo wrote:

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

Nov 20 '05 #6

P: n/a
Hi Tom,

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

Cor
Nov 20 '05 #7

P: n/a
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

Tom Shelton wrote:
On Tue, 18 May 2004 14:34:44 -0700, Frank Rizzo wrote:

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

Nov 20 '05 #8

P: n/a
In article <OS**************@tk2msftngp13.phx.gbl>, Frank Rizzo wrote:
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

Tom Shelton wrote:
On Tue, 18 May 2004 14:34:44 -0700, Frank Rizzo wrote:

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


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 :)

--
Tom Shelton [MVP]
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.