Load and save Color value

江枫思渺然 提交于 2019-12-24 16:22:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!