-
If If If If
I'm trying to find colour's in an image. Its working but what im trying to say is....
Code:
If GetPixel(pctMain.hdc, x, y) = 15663343 or 65280 or 65535 Then
The problem I am having is its using up way too much cpu usage.
A little help would be good :D
Code:
Private Sub Command1_Click()
Dim x As Long, y As Long
If Not LoadPNGFile(App.Path & "\test.png", pctMain) Then
MsgBox "There was an error loading the test.png file", vbCritical, "Error"
End If
For x = 103 To Width - 103
For y = 10 To 300
If GetPixel(pctMain.hdc, x, y) = 65280 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 15663343 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 65025 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 65535 Then
MsgBox "Colour Found"
GoTo Found
Else
If GetPixel(pctMain.hdc, x, y) = 255 Then
MsgBox "Colour Found"
GoTo Found
Else
End If
End If
End If
End If
End If
Next
Next
-
Re: If If If If
You can make it five times faster if you store the result of GetPixel(pctMain.hdc, x, y) into a temporary Long variable before the series of If statements. Thus:
Code:
lngColor = GetPixel(pctMain.hdc, x, y)
If lngColor = 65280 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 15663343 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 65025 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 65535 Then
MsgBox "Colour Found"
GoTo Found
ElseIf lngColor = 255 Then
MsgBox "Colour Found"
GoTo Found
End If
You could also store the hDC value temporarily in a Long variable. However, that's the most you can do while still using GetPixel. If you need even more speed, then the only way is to handle raw bitmap data in a long array or a byte array.
-
Re: If If If If
Thank you, you have been a great help.
Regards
Nathum
-
Re: If If If If
Note that an alternative is to use a Select Case instead of multiple If's, eg:
Code:
Select Case GetPixel(pctMain.hdc, x, y)
Case 65280, 15663343, 65025, 65535, 255
MsgBox "Colour Found"
GoTo Found
End If
I think this is one of the cases where Select Case is faster, but I'm sure Merri will correct me if that's wrong!
If after making these changes you are looking for more speed, you will need to do something like Merri suggested at the end of his post.. I'd recommend looking for a tutorial about DIB sections (which in my experience are over 50 times faster than SetPixel/GetPixel).
ps: any chance of seeing the code for LoadPNGFile? (I have a potential use for it) :D
-
Re: If If If If
si_the_geek: with GetPixel in the same loop, it doesn't really matter.
However, if you're looking for PNG stuff: AlphaImage Control, 32bpp DIB Suite and PNG Reader.
-
Re: If If If If
Thankyou all for your help.
This is what I have been using.
Code:
Case 65280 ' Green
GoTo Found
Case 15663343 ' Pink
GoTo Found
' Case 16776960 'Light Blue
' GoTo Found
Ill change it a bit ;) now.
Regards
Nathum