1

I was in the process of using Pixel to do my search between a large image and a small image. I got that one to successfully work but am having trouble getting the lockbits version to do what I want it to do.

Fist I want to loop through the large image finding the first pixel of the smaller image inside it. Then once finding the first pixel to do a math equation to tell me what percentage of accuracy it got off it. In doing so if it meets the criteria to move my mouse to that location on the screen at the first pixel (top most left) of that small image inside the large image. Which works with a great rate of success and is extremely fast compared to GetPixel format.

Problem Cannot get location on the large image of where the beginning Pixel of the small image is and move the mouse to that location. Below is the code:

             Try
        Dim ifnd As Boolean = False
        Dim PreviousX As Integer
        Dim PreviousY As Integer
        PreviousX = MousePosition.X
        PreviousY = MousePosition.Y
        Dim MatchCount As Integer = 0



        Dim oX As Integer = 0
        Dim oi As Integer = 0
        Dim iX As Integer = 0
        Dim iY As Integer = 0
        Dim bmp_original As Bitmap
        Dim ImG As Bitmap
        ImG = PictureBox2.BackgroundImage
        bmp_original = ImG
        Dim bmp_large As Bitmap
        Dim SmG As Image
        SmG = PictureBox1.BackgroundImage
        bmp_large = SmG
        Dim bmg As Bitmap
        'large image
        ImG = BBt
        'small image
        bmg = Abt
        Thread.Sleep(1000)
        'large image
        If BBt Is Nothing Then
            Dim xbit As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)

            Dim g As Graphics = Graphics.FromImage(xbit)
            BBt = xbit
            g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size)
            g.Dispose()
            Thread.Sleep(2000)
        End If

        'small image
        PictureBox5.BackgroundImage = Abt
        'large image
        PictureBox6.BackgroundImage = BBt
        'For value As Integer = 0 To 5
        'For value As Integer = 10 To 0 Step -2
        Thread.Sleep(1000)
        'original image
        Dim oRect As New Rectangle(0, 0, bmg.Width, bmg.Height)
        Dim oBmpData As BitmapData = bmg.LockBits(oRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
        Dim oPtr As IntPtr = oBmpData.Scan0
        Dim oPixels(99) As Integer
        Dim oMaxPix As Integer = bmg.Width + bmg.Height
        Marshal.Copy(oPtr, oPixels, 0, oMaxPix)
        Dim smWidth As Integer
        smWidth = bmg.Width - 1
        'small image
        PictureBox3.BackgroundImage = bmg
        'large image
        Dim lRect As Rectangle = New Rectangle(0, 0, bmp_large.Width, bmp_large.Height)
        Dim lBmpData As BitmapData = ImG.LockBits(lRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
        Dim lPtr As IntPtr = lBmpData.Scan0
        Dim PixelCount As Integer = ImG.Width * ImG.Height
        Dim lPixels(PixelCount - 1) As Integer
        Marshal.Copy(lPtr, lPixels, 0, PixelCount)
        'large image
        PictureBox4.BackgroundImage = ImG

        Dim MathScore As Integer
        Dim MaxScore As Integer = bmg.Height

        'beginning of Marshal Loop
        For i As Integer = 0 To lPixels.GetUpperBound(0)

            If oPixels(0) = lPixels(i) Then
                'we have a match for top left pixel - so compare the other pixels
                Dim PixelsToLeft As Integer = (i Mod ImG.Width) - 1 'pixels to left of 10by10 section of large image
                Dim PixelsToRight As Integer = ImG.Width - PixelsToLeft - smWidth 'pixels to right of 10by10 section of large image
                Dim d As Integer = PixelsToLeft + PixelsToRight 'array distance between right edge of 10by10 section and left edge of next row

                For y As Integer = 0 To 9
                    For x As Integer = 0 To 9
                        Dim oIndex As Integer = (y * 10) + x
                        Dim lIndex As Integer = (i + (y * (d + smWidth))) + x
                        If oPixels(oIndex) = lPixels(lIndex) Then
                            MathScore = MathScore + 1
                            xx = oPixels(0) + 2
                            yy = lPixels(i) + 3
                            SetCursorPos(xx, yy)
                        End If

                    Next



                Next
                If MathScore >= Val(MaxScore / 2.5) Then
                    SetCursorPos(xx, yy)
                    Dim percent As String
                    Dim myDec As Decimal
                    'inttemp = (intData2 * 100) / intData1
                    myDec = Val((MathScore * 100) / MaxScore)
                    myDec = FormatNumber(myDec, 0)
                    percent = myDec & "%"
                    Label16.Text = "Match Score: " & percent
                    Label17.Text = "Math Score: " & MathScore & " out of " & MaxScore
                    Me.ToolStripStatusLabel2.Text = "Completed"
                    Me.Button4.Enabled = True
                    GoTo Foundit
                End If
            End If
        Next
        PictureBox1.Image = (Abt)
        PictureBox2.Image = (BBt)
        ImG.UnlockBits(oBmpData)
        bmg.UnlockBits(lBmpData)

Foundit: mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) Catch End Try

Now if I could figure out how to get the mouse to move on the screen then I would have figured it out. Unfortunately I have been working on this for several days without any success. If you could help I would greatly appreciate it. Thank you in advance.

1 Answers1

0

to move the mouse do this

dim a as new point 
a.x = "the number you want"
a.y = "the number you want"
windows.forms.cursor.position = a