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

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

Сначала я хочу перебрать большое изображение, найдя внутри него первый пиксель меньшего изображения. Затем, найдя первый пиксель, сделать математическое уравнение, чтобы сказать мне, какой процент точности он получил от него. При этом, если он соответствует критериям, чтобы переместить мою мышь в это место на экране в первый пиксель (самый верхний левый) этого маленького изображения внутри большого изображения. Который работает с большим успехом и чрезвычайно быстр по сравнению с форматом 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)

Найдено: mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) Catch End Try

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


person Shawn Churchel    schedule 27.08.2012    source источник


Ответы (1)


чтобы двигать мышью сделать это

dim a as new point 
a.x = "the number you want"
a.y = "the number you want"
windows.forms.cursor.position = a 
person Saeed A Suleiman    schedule 19.07.2014