Распознавание изображений между 2 изображениями vb.net Lockbits

Я пытался использовать Pixel для поиска между большим изображением и маленьким изображением. Я заставил его успешно работать, но у меня не получается заставить версию lock bits делать то, что я хочу.

Кулак Я хочу пройтись по большому изображению, находя первый пиксель меньшего изображения внутри него. Затем однажды нашел первый пиксель для математического уравнения, чтобы сказать мне, какой процент точности он получил. При этом, если это соответствует критериям, переместите мою мышь в это место на экране в первом пикселе (вверху слева) этого маленького изображения внутри большого изображения. Который работает с большой скоростью и чрезвычайно быстр по сравнению с форматом GetPixel.

Проблема Не удается найти местоположение на большом изображении, где находится начальный пиксель маленького изображения, и переместить мышь в это место. Ниже приведен код:

             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

Теперь, если бы я мог понять, как заставить мышь двигаться по экрану, я бы понял это. К сожалению, я работал над этим в течение нескольких дней без какого-либо успеха. Если бы вы могли помочь, я был бы очень признателен. Заранее спасибо.

1 ответ

Чтобы переместить мышь, сделайте это

dim a as new point 
a.x = "the number you want"
a.y = "the number you want"
windows.forms.cursor.position = a 
Другие вопросы по тегам