问题描述
我有一个简单的pixelsearch函数,可以在屏幕上搜索某种Argb颜色。 这已经很好用了,它可以找到带有颜色的像素,但是我想为其添加颜色变化检测。
它应该检测到的像素的颜色有时会从(255,100,100,100)变为(255,110,94,102)或其他(值在改变10点)。现在,Pixelsearch函数应该具有Variationdetection,因此它将检测具有接近相似颜色的像素,因此,不仅应搜索颜色(255,100,100,100),还应搜索(255,101,99,102)。还有更多
是否可以编写代码而不是每种颜色变暗并进行搜索?
那是我已经拥有的代码:
Private Sub BackgroundWorker1_DoWork(sender As Object,e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim xd3 = Color.FromArgb(255,100,100) 'Searching for this color on the screen
Dim b As New Bitmap(2210,1100) 'Position of Bitmap
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(b)
g.CopyFromScreen(Me.Left,Me.Top,b.Size) 'Searching on Screen
For i = 0 To (Me.Width - 0) 'searching each pixel
For j = 0 To (Me.Height - 0) 'searching each pixel
If b.GetPixel(i,j) = xd3 Then 'If pixel has same color that im searching for it will show a messagebox true
MessageBox.Show("true")
End If
Next
Next
End Sub
解决方法
我建议这样:
If Color_Is_In_The_Target_Variations(10,b.GetPixel(i,j),xd3) Then
MessageBox.Show("true")
End If
和
Private Function Color_Is_In_The_Target_Variations(variation As Integer,tested As Color,target As Color) As Boolean
If tested.R >= target.R - variation And tested.R <= target.R + variation And
tested.G >= target.G - variation And tested.G <= target.G + variation And
tested.B >= target.B - variation And tested.B <= target.B + variation Then
Return True
Else
Return False
End If
End Function