How to read a color from registry?

  • Thread starter Thread starter Morten Wennevik
  • Start date Start date
M

Morten Wennevik

Saving a color object to registry will set it's value to something like
"Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
but how do I get this back into a Color object???

Casting it to Color only causes an exception.
 
Can you show some code, please?

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, and MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Saving a color object to registry will set it's value to something like
"Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
but how do I get this back into a Color object???

Casting it to Color only causes an exception.
 
To make a registry entry in HKEY_Current_User\Software\ColorTest

Color testColor = Color.FromArgb(123, 234, 75);
RegistryKey regKey =
Registry.CurrentUser.CreateSubKey("Software\\ColorTest");
regKey.SetValue("Color", testColor);
regKey.Close();

To read it I would want to use something like

RegistryKey colorKey =
Registry.CurrentUser.OpenSubKey("Software\\ColorTest");
Color regColor = (Color)regKey.GetValue("Color"); // this does not work
colorKey.Close();

I can do it by translating the color to int and creating a new color from
int, but I would prefer to keep the color readable in registry.
 
Store the 3 numbers (r,g,b) in registry as one string
value like "234,212,122" and when you retrieve this value,
split it into 3 parts, and then use the Color.FromArgb()
method to recreate a color object.

HTH
Sunil TG
 
Morten,

The easiest way to do this would be to use an instance of the
ColorConverter class. You can call the ConvertToString method on the class
to convert your color instance to a string which you store in the registry.
Then, later on, you can call ConvertFromString on the string you get from
the registry to get your color back.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Saving a color object to registry will set it's value to something like
"Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
but how do I get this back into a Color object???

Casting it to Color only causes an exception.
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ah, that's because you're saving the result of ToString() method to the
registry. Unfortunately, there's no easy way to convert it back to Color
~ other than examining the string--and definitely you can't cast it to
Color because it's a string!

It's better to save the result of calling ToArgb() to the registry. When
you load the int value back and need to instantiate a Color object from
that, you can just say:

Color col = Color.FromArgb(<put the int from the Registry here>);

Morten Wennevik wrote:
| Saving a color object to registry will set it's value to something like
| "Color [Red]", or "Color [A=255, R=123, G=234, B=231]",
| but how do I get this back into a Color object???
|
| Casting it to Color only causes an exception.
|


- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/nnFPwEwccQ4rWPgRAiw4AJ9FYtAbDAsnn2lkNwv5XH6QNldySACfedAF
1OuxNwXHBgtVDRAZ55VQx7M=
=bG0X
-----END PGP SIGNATURE-----
 
Yes, thank you, ColorConverter was rather good.
It will still display the color name if the color has one, otherwise
"123;234;231"
 
Ray,

On top of that, if it is a named color, the return value from the
IsNamedColor property will be preserved.
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I realized that using the Argb way, the name of the colour gets lost
when you want to convert it back into a Color instance. The (better) way
Nicholas suggested preserves it, plus it lets you read the color in the
Registry :)

Thanks!

Nicholas Paldino [.NET/C# MVP] wrote:

| Morten,
|
| The easiest way to do this would be to use an instance of the
| ColorConverter class. You can call the ConvertToString method on the
class
| to convert your color instance to a string which you store in the
registry.
| Then, later on, you can call ConvertFromString on the string you get from
| the registry to get your color back.
|
| Hope this helps.

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/nnhSwEwccQ4rWPgRApxdAJ4zBh1FJomnA7GBXGYaPJZhTwfLSQCfYgJh
eanTtqlfPxZhuhbiRFn8lRE=
=a4dj
-----END PGP SIGNATURE-----
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oh, another thing I didn't know :) Thanks Nicholas!

Nicholas Paldino [.NET/C# MVP] wrote:

| Ray,
|
| On top of that, if it is a named color, the return value from the
| IsNamedColor property will be preserved.

- --
Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/noR7wEwccQ4rWPgRAk5ZAJ4t1MWBraPzVgu8ONl4L/1WmR059wCdFc/J
zIoqLjcKWRs2VCCOsUJpYkY=
=XDU8
-----END PGP SIGNATURE-----
 
Back
Top