I am using a WPF Color Picker that returns a Color. However, I struggle with how to save that color as an integer and load into the databse. I found a few solutions, none of them worked and all were quite complicated (and old). Hopefully there is a newer, more elegant solution. THanks in advance
Asked
Active
Viewed 1,444 times
1
-
do you mean saving the RGBA value as an int? so like 255,255,255,255 would be 255255255255? – psoshmo Aug 21 '14 at 19:46
-
For example, I just need to be able to save a value into the DB and then load it and use it for font colors. – John V Aug 21 '14 at 19:47
-
just use a char type and save the hexadecimal like "#FFFFFF" – psoshmo Aug 21 '14 at 19:48
-
Not really sure how to do that - never worked with colors. – John V Aug 21 '14 at 19:50
-
ok, so what is the end goal of these colors? are you gonna be pulling them out of the database later and doing things with them. Plus, can you show the code where you get the color from the picker? – psoshmo Aug 21 '14 at 19:55
-
Color c1 = Color.Beige; int i = c.ToArgb(); Color c2 = Color.FromArgb(i); – TaW Aug 21 '14 at 20:03
-
@TaW cool I did not know about those methods :) learn something new everyday. – psoshmo Aug 21 '14 at 20:07
-
Well, they're in `System.Drawing` not quite sure if they WPF compatible, though.. – TaW Aug 21 '14 at 20:10
1 Answers
0
I would recommend using the hex code over just an int of the RGB values.
This following code is taken from here and belongs to user @AriRoth
private static String HexConverter(System.Drawing.Color c)
{
return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
}
This method should convert your color into a string of the hex code. You can then save that into your database. If you plan on using these for CSS purposes later, the hex code will be enough for that. If you need to convert the hex back into a color object for whatever reason, you can use
using System.Windows.Media;
Color color = (Color)ColorConverter.ConvertFromString(YourHexCodeHere);
-
-
@user970696 sorry had to leave work shortly after posting. yes the type in the db could be varchar or char, eitherway. If this answer helped, feel free to upvote it/accept it. – psoshmo Aug 22 '14 at 12:28