问题
I'm trying to load Color value from a textfile with ReadLine. When saved with WriteLine it looks like this [Color: R=53, G=40, B=121, A=255, PackedValue=4286130229].
Color color = Color.Black;
stream.WriteLine(color.ToString());
Then I guess it has to be converted somehow, cant figure it out. color ??? stream.ReadLine();
回答1:
If you're using a text storage medium, then go with HTML encoding:
Color myColor = Color.FromArgb(255, 255, 50, 25);
String strColor = ColorTranslator.ToHtml(myColor);
//write strColor to text file...
String strColor = ""; //read in the color from text file
Color c = ColorTranslator.FromHtml(strColor);
With this, you will store a text string that can be either a hex (#FFFFFF) or a known color name (Black). The method chooses automatically based on whether your Color
object is known or custom RGB. Either way, it should translate back-and-forth nicely.
回答2:
What you are trying to do is serializing/deserializing. You should look into that. But you could save a line for each of your colors, and make each line a semicolon separated list of RGB values, that's almost what your are doing now.
Then when you read your lines, you will have to deserialize it with something like
var colorStr = stream.ReadLine().Split(',');
Color clr = Color.FromArgb(colorStr[0],colorStr[1],colorStr[2]);
来源:https://stackoverflow.com/questions/22437558/load-and-save-color-value