本文介绍了加载并保存颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从ReadLine的文本文件中加载颜色值。当与WriteLine保存时,它看起来像这样[颜色:R = 53,G = 40,B = 121,A = 255,PackedValue = 4286130229]。

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());

然后我想它必须以某种方式转换,
color ??? stream.ReadLine();

Then I guess it has to be converted somehow, cant figure it out.color ??? stream.ReadLine();

推荐答案

如果您使用的是文本存储介质,请使用HTML编码:

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);

这样,您将存储一个文本字符串,可以是十六进制(#FFFFFF)已知颜色名称(黑色)。该方法根据您的 Color 对象是否已知或自定义RGB自动选择。无论如何,它应该很好地翻译。

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.

这篇关于加载并保存颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:03