Live Chat Icon For mobile
Live Chat Icon

I need to save a color as a string and be able to retrieve it. How can I do this

Platform: WinForms| Category: Colors

Here are a couple of routines that might do what you want. ColorToString
takes a color and represents it as a string that then can be passed into its
companion StringToColor routine that will take the string back into a color.
I think it works with all types of colors. You can download a test project.

public string ColorToString(Color c)
  string s = c.ToString();
  s = s.Split(new char[]{’[’,’]’})[1];
  string[] strings = s.Split(new char[]{’=’,’,’});
  if(strings.GetLength(0) > 7)
    s = strings[1] + '','' + strings[3] + '','' + strings[5] + '','' + strings[7];
  return s;

public Color StringToColor(string s)
  return (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(s); 

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.