[英]Casting a RGB string to a Color type
我正在讀取具有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);
}
}
我想將這些當前的String
轉換為Color
值,因為它們只是String形式的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[]
中?
查看Color.FromArgb
的重載,它們都希望將int
傳入。因此,不能,您不能只是傳遞一個字符串並期望它可以工作。 但是,將字符串轉換為一組整數並不難。
public Color[] convertToColorArray() {
for (int i = 0; i < rows; i++) {
//This gives us an array of 3 strings each representing a number in text form.
var splitString = stringArray[i].Split(',');
//converts the array of 3 strings in to an array of 3 ints.
var splitInts = splitString.Select(item => int.Parse(item)).ToArray();
//takes each element of the array of 3 and passes it in to the correct slot
colorArray[i] = System.Drawing.Color.FromArgb(splitInts[0], splitInts[1], splitInts[2]);
}
return colorArray;
}
所有這些代碼都假定您的源文件格式正確,因此string.Split
將始終返回至少3個數組,而int.Parse
將永遠不會失敗解析輸入。
您嘗試調用的函數需要1-4個參數
http://msdn.microsoft.com/zh-cn/library/system.drawing.color.fromargb%28v=vs.110%29.aspx
假設您確定每一行都有三個值-這應該可以工作
string[] splitArray = stringArray[i].Split(',');
System.Drawing.FromARGB(Int32.Parse(splitArray[0]),Int32.Parse(splitArray[1]),Int32.Parse(splitArray[2]);
如果事先將Array轉換為int []的數組,則可以編寫此清潔器
int[] intArray = splitArray.Select(sa => Int32.Parse(sa)).ToArray();
那么您就可以調用intArray [0]等。
像這樣嗎
var colors = File.ReadLines(fname)
.Select(line => line.Split(','))
.Select(p => Color.FromArgb(byte.Parse(p[0]), byte.Parse(p[1]), byte.Parse(p[2])))
.ToList();
我使用string的擴展方法來處理透明的后備而不拋出異常:
private static System.Drawing.Color ToColor(this string color)
{
var arrColorFragments = color?.Split(',').Select(sFragment => { int.TryParse(sFragment, out int fragment); return fragment; }).ToArray();
switch (arrColorFragments?.Length)
{
case 3:
return System.Drawing.Color.FromArgb(arrColorFragments[0], arrColorFragments[1], arrColorFragments[2]);
case 4:
return System.Drawing.Color.FromArgb(arrColorFragments[0], arrColorFragments[1], arrColorFragments[2], arrColorFragments[3]);
default:
return System.Drawing.Color.Transparent;
}
}
請注意,我使用的是C#7。也許您必須對正在使用的C#版本進行調整。
您需要獲取字符串,並用逗號定界符將其分割,然后為每個字符串最后將其轉換為int,最后將其放入FromArgb方法中。
FromArgb方法期望單個ARGB Int32值,而不是您想要的值。 嘗試以下方法:
var items = stringArray[i].Split(",").Select(k => int.Parse(k)).ToArray();
colorArray[i] = Color.FromArgb(items[0], items[1], items[2]);
FromArgb有一個重載方法,該方法接受3個整數作為參數,一個用於R,一個用於G,一個用於B。
將您的stringArray [i]分成3個整數部分(應該很容易,bcoz用逗號分隔),並將它們傳遞給方法。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.