Hi, i'm fairly new to .NET after getting tired of using VB6 due to the lack of functionality. I'm trying to create a log on method for a system which will involve users selecting a colour from a paint panel and then placing/drawing the colour in 1 or more tiles of a grid, basically a 3x3 grid in which for example they selected the blue colour and drew in the top left tile and the bottom left tile. The logon is selecting the correct colour and placing the colour in the appropriate tile. What i want them to be able to do is place even a single pixel of colour so it has a high accuracy. So far i have a timer which loops from the width of the grid to the height of the grid.

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Dim BMP As New Drawing.Bitmap(1, 1)
        Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
        GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), _
                           New Drawing.Point(0, 0), BMP.Size)

        For i = 0 To BMP.Width - 1
            Dim Pos As Point
            For j = 0 To BMP.Height - 1
                Dim Pixel As Drawing.Color = BMP.GetPixel(i, j)
                Pos.X = i
                Pos.Y = j

                Windows.Forms.Cursor.Position = New System.Drawing.Point(i, j)
                If Pixel.R = 255 And Pixel.G = 174 And Pixel.B = 201 Then
                    MsgBox("Colour found " & Pixel.R)
                    Timer1.Enabled = False
                End If
                

            Next
        Next
        MsgBox("Not found")
        Timer1.Enabled = False
    End Sub

To start with i have just hard coded the colour i want it to detect. At present it produces an error. Parameter must be positive and < Width the error is on System.Drawing.Point(i,j). If i place the mouse over the colour then run the program it will say it has been found, but otherwise it wont find the colour. I believe i am not updating the location of the pixel or something to that effect.
Thanks in advance
TG

If the variable creation is in the loop, you are not disposing the bitmap before trying to create another with the same name.

okay thanks, so declare it beforehand and then reset it with the updated pixel location?

causes it too error, Parameter must be positive and < Height.
Parameter name: y

Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
        For i = 0 To My.Computer.Screen.WorkingArea.Width - 1
            Dim Pos As Point
            For j = 0 To My.Computer.Screen.WorkingArea.Height - 1
                Pixel = BMP.GetPixel(i, j)

in the bottom line j

I have just one question though.

Is a color dialog out of the picture for your program?

If not, it would make your life SO much easier.


Other than that, what is the height of BMP?

Edited 4 Years Ago by Begginnerdev: n/a

Color dialog is not out the picture, as long as it can detect the correct colour picture. The BMP.Height is currently 1, this is because i delare it as (1,1). Should i change this to My.Computer.Screen.workingarea.height and .width?

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Dim BMP As New Drawing.Bitmap(My.Computer.Screen.WorkingArea.Width, My.Computer.Screen.WorkingArea.Height)
        Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
        GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), _
                          New Drawing.Point(0, 0), BMP.Size)
        Dim Pixel As Drawing.Color '= BMP.GetPixel(0, 0)
        For i = 0 To My.Computer.Screen.WorkingArea.Width - 1
            For j = 0 To My.Computer.Screen.WorkingArea.Height - 1
                Pixel = BMP.GetPixel(i, j)
                GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), _
                           New Drawing.Point(0, 0), BMP.Size)
                Windows.Forms.Cursor.Position = New Point(i, j)

                If Pixel.R = 255 And Pixel.G = 201 And Pixel.B = 14 Then
                    MsgBox("Colour found " & Pixel.R)
                    MsgBox(i & j)
                    End
                End If
            Next
        Next
        MsgBox("Not found")
        Timer1.Enabled = False
    End Sub
End Class

Okay so i've now got it working with searching for the colour, but the problem is the search speed, for one complete pass will take 30 minutes or so which is a stupid amount of time, any ideas? i could step by 2 - 4 on each loop which may help. How would your idea using color dialog work?

You can add a color dialog to your project, and pull the color from that.

A color dialog holds a custom color picker.

As for the scanning, if you are looping through the whole width/height of the screen it will take a long time. The only other way to fix other than the stepping will be to reduce your resolution.

Hmm okay, well I've increased the stepping which has sped it up, suppose a lower res or a smaller area is probably a better way to do it. Cheers

This question has already been answered. Start a new discussion instead.