473,545 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6852
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.Serializati on.XmlSerialize r(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.F ormatters.Binar y

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

Try
bf.Serialize(me m, fnt)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

' again, very basic handling..
Private Function DeserializeFont Object(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(fnt))

Try
Return DirectCast(bf.D eserialize(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.F ormatters.Binar y

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

Try
bf.Serialize(me m, fnt)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

' again, very basic handling..
Private Function DeserializeFont Object(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(fnt))

Try
Return DirectCast(bf.D eserialize(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 SerializeAnyFre akingObject (Of T)
'as long it supports serialization, of course

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

Try
bf.Serialize(me m, oObject)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

Private Function DeserializeFont Object(ByVal sString As String) As T
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(sString ))

Try
Return DirectCast(bf.D eserialize(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.F ormatters.Binar y

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

Try
bf.Serialize(me m, fnt)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

' again, very basic handling..
Private Function DeserializeFont Object(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(fnt))

Try
Return DirectCast(bf.D eserialize(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 SerializeAnyFre akingObject (Of T)
'as long it supports serialization, of course

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

Try
bf.Serialize(me m, oObject)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

Private Function DeserializeFont Object(ByVal sString As String) As T
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(sString ))

Try
Return DirectCast(bf.D eserialize(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.F ormatters.Binar y

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

Try
bf.Serialize(me m, fnt)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function

' again, very basic handling..
Private Function DeserializeFont Object(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(fnt))

Try
Return DirectCast(bf.D eserialize(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
5812
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 brand new object when deserializing. In my case I have an existing object that I'd like to pass some XML to for the object to repopulate its member...
14
14278
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
24675
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
0
2162
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 and properties derived from this class but will not serialize or deserialize the properties in CollectionBase. Like InnerList, which is a read only...
2
2418
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 MSDN example. The CODE is BELOW this lines. If you see something wrong or missing please answer. Class declaration:
0
2059
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> <Size>
2
35587
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 the saved settings Can anyone see something that I have missed ?
4
7089
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 Building._Name to Building._BName in case the duplicate name was the issue, but that didn't help. Is there a native way to serialize nested...
4
2194
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 Function, and Deserialize Function, but the thing is that when trying to Serialize and then Deserialize on the same project it's working perfectly, but...
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.