Home All Groups Group Topic Archive Search About

how to persist color settings in my app?

Author
6 Jan 2006 1:36 AM
Terry Olsen
I want to be able to save the user color settings in my app and restore them
when the app is re-opened.  I currently have a config file with different
settings such as:

[App Settings]
UID=jsmith
IP=127.0.0.1
Port=23

I want to be able to save the color settings in the same way, but the I
can't convert from System.Drawing.Color to String.  How can I accomplish
this?

Thanks.

Author
6 Jan 2006 9:04 AM
Cor Ligthert [MVP]
Terry,

The in my opinion most simple way is the "Color.ToArgb" and the
Color.FromArgb" methods.

http://msdn2.microsoft.com/en-us/library/system.drawing.color.toargb.aspx

I hope this helps,

Cor
Author
7 Jan 2006 5:45 AM
Terry Olsen
Thanks! That worked great.  Now I can't get my font to persist.  I tried
writing out the font as Font.ToHfont and then reading it back in as
Font.FromHfont, but I get an error saying "This only works with TrueType
Fonts. This isn't a TrueType Font."  The font actually is a TrueType font,
at least it has the .ttf suffix and has the TT logo next to its name in the
listing.

How can I persist my font?

Thanks.

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:eTYHsAqEGHA.3820@TK2MSFTNGP12.phx.gbl...
> Terry,
>
> The in my opinion most simple way is the "Color.ToArgb" and the
> Color.FromArgb" methods.
>
> http://msdn2.microsoft.com/en-us/library/system.drawing.color.toargb.aspx
>
> I hope this helps,
>
> Cor
>
>
Author
7 Jan 2006 9:24 AM
Cor Ligthert [MVP]
Terry,

You are even able to serialize a font.

\\\first showed to me by Tom Shelton
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


    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

I hope this helps,

Cor
Author
7 Jan 2006 3:18 PM
Terry Olsen
Yup, that did it.  Thanks!

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:OhC4Gw2EGHA.1816@TK2MSFTNGP11.phx.gbl...
> Terry,
>
> You are even able to serialize a font.
>
> \\\first showed to me by Tom Shelton
> 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
>
>
>    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
>
> I hope this helps,
>
> Cor
>
Author
7 Jan 2006 12:04 PM
Herfried K. Wagner [MVP]
"Terry Olsen" <tolse***@hotmail.com> schrieb:
> Now I can't get my font to persist.  I tried writing out the font as
> Font.ToHfont and then reading it back in as Font.FromHfont, but I get an
> error saying "This only works with TrueType Fonts. This isn't a TrueType
> Font."

In addition to the other replies:  The font handle you obtain using
'ToHfont' points to the font object the method is called on.  This handle
won't exist any more if you restart the application.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>