문제

This question is related to Visual Basic .NET 2010

Okay so, I made this program that can redraw an image on any surface on the screen. I'm using some Win32 API to move the mouse and simulate clicks and so on.

The thing is, I used to just make it click for every pixel, which resulted in a lot of lag when used on a flash or javascript surface.

I need to detect "lines" of pixels, as in, if I'm enumerating the pixels and checking their color, if the current pixel is black, and the next 10 ones are black as well, I need to be able to detect it and draw a line instead of clicking each one, in order to prevent lag.

Here's my current code, it's how I enumerate the pixels.

Private Sub Draw()
    If First Then
        Pos = New Point(Me.Location)
        MsgBox("Position set, click again to draw" & vbCrLf _
               & "Estimated time: " & (Me.BackgroundImage.Width * Me.BackgroundImage.Height) / 60 & " seconds (1ms/pixel)")
        First = False
    Else
        Using Bmp As New Bitmap(Me.BackgroundImage)
            Using BmpSize As New Bitmap(Bmp.Width, Bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb) 'Use to get size of bitmap
                For y As Integer = 0 To BmpSize.Height - 1
                    For x = 0 To BmpSize.Width - 1
                        Dim curPixColor As Color = Bmp.GetPixel(x, y)
                        If curPixColor = Color.White Then Continue For 'Treat white as nothing
                        If IsColorBlack(curPixColor) Then
                            'TODO:
                            'If more than 1 black pixel in a row, use _Drag() to draw line
                            'If 1 pixel followed by white, use _Click() to draw 1 pixel
                        End If
                    Next
                Next
                MsgBox("Drawn")
                First = True 'Nevermind this, used for resetting the program
            End Using
        End Using
    End If
End Sub

Private Sub _Drag(ByVal From As Point, ByVal _To As Point)
    SetCursorPos(From.X, From.Y)
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    SetCursorPos(_To.X, _To.Y)
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

Private Sub _Click(ByVal At As Point)
    SetCursorPos(At.X, At.Y)
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

As always, help is much appreciated. It's a rather complicated question but I hope I made some sense.

도움이 되었습니까?

해결책

You can try to count it like this

'Count of the black pixels
Dim BlackCount As Integer = 1
'Another intermediate X variable
Dim ThisX As Integer = x + 1
'Run along until reaching the right edge or a not black pixel
While ThisX < Bmp.Width AndAlso IsColorBlack(Bmp.GetPixel(ThisX, y))
    BlackCount += 1
    ThisX += 1
End While
'Act accordingly
If BlackCount > 1 Then
   'Drag from x to Thisx-1
Else
   'Click
End If
x = ThisX 'Update the X variable to skip over the covered area

Also try to determine what causes the lag. GetPixel and SetPixel are extremely slow. To improve the performace look into the LockBits way of reading pixel values. Try google or http://msdn.microsoft.com/de-de/library/ms229672%28v=vs.90%29.aspx for a first start. It is by magnitudes faster and should be used when reading any significant amount of pixels.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top