1

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)

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

マウスを画面上で移動させる方法を理解できれば、それを理解できたでしょう。残念ながら、私はこれに数日間取り組んできましたが、成功しませんでした。お役に立てれば幸いです。前もって感謝します。

4

1 に答える 1