Board index » Visual Studio » Serialize/Deserialize Font to/from string
|
Bluedot
|
|
Bluedot
|
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. - |
| Sven
Registered User |
Tue May 18 18:17:38 CDT 2004
Re:Serialize/Deserialize Font to/from string
Frank Rizzo wrote:
QuoteHow do I serialize Font object into a string that I can store in -- Sven Groot unforgiven.bloghorn.com">unforgiven.bloghorn.com - |
| Frank
Registered User |
Tue May 18 18:40:14 CDT 2004
Re:Serialize/Deserialize Font to/from string
Sven Groot wrote:
QuoteFrank Rizzo wrote: Font object does not have a default constructor, it can't do anything with it. Dim oXml As New Xml.Serialization.XmlSerializer(GetType(Font)) - |
| Tom
Registered User |
Wed May 19 00:42:24 CDT 2004
Re:Serialize/Deserialize Font to/from string
On Tue, 18 May 2004 14:34:44 -0700, Frank Rizzo wrote:
QuoteHow do I serialize Font object into a string that I can store in either 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 - |
| Cor
Registered User |
Wed May 19 01:41:56 CDT 2004
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 - |
| Frank
Registered User |
Wed May 19 19:29:49 CDT 2004
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: QuoteOn Tue, 18 May 2004 14:34:44 -0700, Frank Rizzo wrote: |
| Cor
Registered User |
Thu May 20 01:13:52 CDT 2004
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 - |
| Frank
Registered User |
Fri May 21 13:20:45 CDT 2004
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: QuoteOn Tue, 18 May 2004 14:34:44 -0700, Frank Rizzo wrote: |
| Tom
Registered User |
Fri May 21 14:41:30 CDT 2004
Re:Serialize/Deserialize Font to/from string
In article <OSt3lA2PEHA.2716@tk2msftngp13.phx.gbl>, Frank Rizzo wrote:
QuoteBtw, this sample will be awesome in VS2005 with generics the type to object (and again, as long as the object supports serilization :) -- Tom Shelton [MVP] - |
