2014-04-25 89 views
0

我使用g.CopyFromScreen(screenpoint,Point.Empty,Bmp2.Size)(当前的dropper工具)使用颜色吸管工具制作应用程序,一旦我有吸管值我想将RBG值转换为单个整数。将RGB值转换为整数

我正在转换中的值是在该格式 颜色[A = 255,R = 240,G = 240,B = 240] ,其需要在四个不同的整数

我的代码是让我奇怪的结果,现在我失去了

我的代码:

Dim text1Conv As String 

    text1Conv = TextBox1.Text 
    Dim myChars() As Char = text1Conv.ToCharArray() 

For Each ch As Char In myChars 
     If Char.IsDigit(ch) And Not ch = " " And Not ch = "," And Not count > 2 Then 
      color1Conv = color1Conv + ch 
      TextBox2.Text = TextBox2.Text + color1Conv 'test result 
      count = count + 1 
     ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 2 And Not count > 5 Then 
      color2Conv = color2Conv + ch 
      TextBox2.Text = TextBox2.Text + color2Conv 'test result 
      count = count + 1 
     ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 5 And Not count > 8 Then 
      color3Conv = color3Conv + ch 
      TextBox2.Text = TextBox2.Text + color3Conv 'test result 
      count = count + 1 
     ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 8 And Not count > 11 Then 
      color4Conv = color4Conv + ch 
      TextBox2.Text = TextBox2.Text + color4Conv 'test result 
      count = count + 1 
     End If 

    Next 

结果:225 255 118 112 122 结果:225 255 116 772 721

可能容易的,但我不能看到它

+0

到底是什么Textbox1.Text值的格式?它是颜色[A = 255,R = 240,G = 240,B = 240]? –

+0

它应该看起来像这样225255118112作为一个测试结果,4个不同的整数... textbox1只是一个测试输出fyi – user3037472

+0

它会始终用零填充以防数字低于100,即总是会有12个数字? –

回答

1

使用正则表达式: 我使用的 “[A = 255,R = 241,G = 24,B = 2]” 作为测试字符串和分裂它分成四个整数。

Dim a as Integer, r as Integer, g as Integer, b as Integer 
Dim s as String = "[A=255, R=241, G=24, B=2]" 
Dim mc as MatchCollection = System.Text.RegularExpressions.Regex.Matches(s, "(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+", RegexOptions.None) 
Integer.TryParse(mc(0).Groups(1).Value, a) 
Integer.TryParse(mc(0).Groups(2).Value, r) 
Integer.TryParse(mc(0).Groups(3).Value, g) 
Integer.TryParse(mc(0).Groups(4).Value, b) 

注意:它将不会有任何问题,数字是1,2或任何数字的数字长。

0

您可以使用正则表达式:

Imports System.Text.RegularExpressions 

Dim input As String = "Color [A=255, R=240, G=240, B=240]" 
Dim re As New Regex("Color \[A=(\d+), R=(\d+), G=(\d+), B=(\d+)\]") 
Dim m As Match = re.Match(input) 
Dim integer1 As Integer = Convert.ToInt32(m.Groups(1).Value) '255 
Dim integer2 As Integer = Convert.ToInt32(m.Groups(2).Value) '240 
Dim integer3 As Integer = Convert.ToInt32(m.Groups(3).Value) '240 
Dim integer4 As Integer = Convert.ToInt32(m.Groups(4).Value) '240