【发布时间】:2013-12-24 09:31:27
【问题描述】:
我正在读取具有 RGB 值列表的文件,即
0,1,6
0,2,6
0,43,170
0,42,168
0,44,175
0,44,176
0,44,176
0,221,255
0,222,255
0,222,255
我已使用此构造函数将所有这些值存储到 string[] 数组中:
public Program(int rows, String fileLocation) {
int i;
String line;
count = 0;
this.rows = rows;
this.fileLocation = fileLocation;
stringArray = new String[rows];
try {
System.IO.StreamReader file = new System.IO.StreamReader(fileLocation);
for (i = 0; i < rows; i++) {
while ((line = file.ReadLine()) != null) {
stringArray[i] = line;
count++;
break;
}
}
}
catch (Exception e) {
throw (e);
}
}
我想将这些当前的 Strings 转换为 Color 值,因为它们只是字符串形式的 RGB 值。
所以我用了这个方法:
public Color[] convertToColorArray() {
for (int i = 0; i < rows; i++) {
colorArray[i] = System.Drawing.Color.FromArgb(stringArray[i]);
}
return colorArray;
}
话虽如此,我收到以下错误:
告诉我我的参数无效。我知道这个参数不一定是这样的255,255,255,它们是三个用逗号分隔的ints,但我的string 输入是这种格式。我应该怎么做?我应该把它投射到什么东西上吗?我应该在一开始就将这些值存储到构造函数中的Color[] 中吗?
【问题讨论】:
-
你需要把你的字符串解析成int,然后再拆分
标签: c# colors system.drawing