Board index » Visual Studio » Serialize/Deserialize Font to/from string

Serialize/Deserialize Font to/from string

Visual Studio341
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.


-
 

Re:Serialize/Deserialize Font to/from string

Frank Rizzo wrote:

Quote
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



unforgiven.bloghorn.com">unforgiven.bloghorn.com

-

Re:Serialize/Deserialize Font to/from string

Sven Groot wrote:

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



-

Re:Serialize/Deserialize Font to/from string

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



Quote
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

-

Re:Serialize/Deserialize Font to/from string

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





-

Re:Serialize/Deserialize Font to/from string

Thanks, Tom.



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



Tom Shelton wrote:

Quote
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

-

Re:Serialize/Deserialize Font to/from string

Hi Tom,



I was mixed up with XML serializing, however thanks for the sample, I did

not know this one.



Cor





-

Re:Serialize/Deserialize Font to/from string

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:

Quote
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

-

Re:Serialize/Deserialize Font to/from string

In article <OSt3lA2PEHA.2716@tk2msftngp13.phx.gbl>, Frank Rizzo wrote:

Quote
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]

-