473,382 Members | 1,329 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,382 software developers and data experts.

Serialize/Deserialize Font to/from string

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
8 6829
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
0
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
2
by: alexandre martins | last post by:
Every time i try to make Deserialize the computer gives me the folowing error: "End of Stream encountered before parsing was complete" the code that i'm running is simple and is based on an...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
2
by: Joe | last post by:
Hi I have a Generics List in a PropertyGrid I am able to Serialize it to XML but when I try to deserialize back to the class of the PropertyGrid The Constructor doesn't seem to fire to reload...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
4
by: djidan | last post by:
hi, i am a newbe to c#, i'm trying to send en custom object from a client to a server, after reading a lot on the web ifound that i need to use: BinaryFormatter, MemoryStream, and Serialize...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.