SRL Archive - C#/Visual Basic Help and Tutorials

[vb.net]my findcolor Function


SlashShot

2007-12-22 19:22:20

basically what the title says.

Private Function findColor(ByVal Color As Color, ByVal Area As Rectangle) As Point
Dim AreaSize As Size = Area.Size
Dim TempBitmap As New Bitmap(AreaSize.Width, AreaSize.Height)
Graphics.FromImage(TempBitmap).CopyFromScreen(Area.Location, New Point(), AreaSize)
For x As Integer = 0 To Area.Height - 1
For y As Integer = 0 To Area.Width - 1
If TempBitmap.GetPixel(x, y) = Color Then Return New Point(Area.X + x, Area.Y + y)
Next
Next
Return New Point(-1, -1)
End Function

for actually getting the Color to find:
or using a button where you press down, and release over the selected color, use this as an example
in this case, a picturebox called PictureBoxColor has its background color changed to the hovered over color.

	Private Sub btnGetColor_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnGetColor.MouseDown
tmrGetColor.Start()
End Sub


Private Sub tmrGetColor_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGetColor.Tick
Dim TempBitmap As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Graphics.FromImage(TempBitmap).CopyFromScreen(Screen.PrimaryScreen.Bounds.Location, New Point(), Screen.PrimaryScreen.Bounds.Size)
PictureBoxColor.BackColor = TempBitmap.GetPixel(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y)
End Sub

Private Sub btnGetColor_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnGetColor.MouseUp
tmrGetColor.Stop()
End Sub

R0b0t1

2007-12-23 22:57:23

Using get pixel isn't the best idea, as its really slow. .NET makes it even slower.

SlashShot

2007-12-24 17:14:29

its not get pixel though, it gets a screenshot, and then gets the pixel, not like the traditional vb6 version. I don't believe that .net will make it any slower.
I will probably set up a GetDIBits or LockBits version that will run faster but not as of now.

R0b0t1

2007-12-24 21:12:10

.NET makes it slower by checking each line of code. Ever wonder why programs avoid creating a BSOD when errors occur?


And it still is getpixel, but on a picture, which is still slow.