开发者

storing and retrieving colors with database, C# windows forms application

I'm making a windows application with C#. I'm using the color dialog box for the user to select a color. I'd like to store that color 开发者_开发百科in a database, and be able to retrieve it later on, and be able to use that color in the user interface.

What approach would you suggest to me?


The best way will be to store the hex color in a database field nvarchar(7) ... the input would be #ffffff as an example. varchar(6) would work just as well, and take up less space in your DB. Just be sure to append the # in your code.

Since you need to convert it to/from a control color, you can use System.Drawing.ColorTranslator.FromHtml(someHexColor)

// Hex to Control Color
var myColor = "#[color from database]";
var myControlColor = System.Drawing.ColorTranslator.FromHtml(myColor);

// Control Color to Hex
var colorBlue = System.Drawing.Color.Blue;
var hexBlue = System.Drawing.ColorTranslator.ToHtml(colorBlue);


I use the functions System.Drawing.Color.FromArgb() and System.Drawing.Color.ToArgb() to convert the color from and to integer, and save it as int on the database


i found a way to get the hex code of a selected color with ColorDialog

ColorDialog col = new ColorDialog();

col.ShowDialog();

string color = col.Color.ToArgb().ToString("x");

color = color.Substring(2, 6);

color = "#" + color;

MessageBox.Show(color);

button1.BackColor = System.Drawing.ColorTranslator.FromHtml(color);


Way after the question was originally asked, but this might be useful. It allows you to convert between Color and String in a single function call, no matter whether the color is a SystemColor, a KnownColor, or straight ARGB. Code:

        public static Color Str2Color(string cS, Color oDefault)
        {
            string cColor;
            Color oColor;

            try
            {
                if ((cS == null) || (cS == ""))
                {
                    return oDefault;
                }
                if (cS.StartsWith("[SystemColor]"))
                {
                    cColor = cS.Substring(13);
                    oColor = Color.FromName(cColor);
                    return oColor;
                }
                else if (cS.StartsWith("[KnownColor]"))
                {
                    cColor = cS.Substring(12);
                    oColor = Color.FromName(cColor);
                    return oColor;
                }
                else if (cS.StartsWith("[ARGB]"))
                {
                    cColor = cS.Substring(6);
                    oColor = Color.FromArgb(Convert.ToInt32(cColor));
                    return oColor;
                }
                else
                    throw new IndexOutOfRangeException();
            }
            catch (Exception ex)
            {
                return oDefault;
            }
        }


        public static string Color2Str(Color oColor)
        {
            // To note:
            // 1. System.Windows.SystemColors => [SystemColor]
            //      Examples: SystemColors.Control, SystemColors.Window, SystemColors.WindowFrame. These are synonymous with System.Drawing.SystemColors, and are defined in the Windows style selected by the user.
            // 2. Windows.UI.Colors => [KnownColor]
            //      Examples: Colors.AliceBlue, Colors.AntiqueWhite, Colors.Aqua, Colors.Aquamarine etc. These are named colors that I think are the same as those expressable in HTML
            // 3. ARGB => [ARGB]
            //      Example: #ffff77 - hex representation of the RGB colour scheme.
            string cColor;

            
            try
            {
                if (oColor.IsSystemColor)
                {
                    cColor = "[SystemColor]" + oColor.Name;
                }
                else if (oColor.IsKnownColor)
                {
                    cColor = "[KnownColor]" + oColor.Name;
                }
                else
                {
                    cColor = "[ARGB]" + oColor.ToArgb();
                }

                return cColor;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

I put it in a static "helper" class. Means you can store the value in a database in a human readable format, and human editable format (as long as you get the name right).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜