|
-
Jun 22nd, 2007, 07:11 PM
#1
Thread Starter
Lively Member
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 
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
Evolution is just so unlikely to produce complex life forms.

-
Jun 22nd, 2007, 07:36 PM
#2
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.
-
Jun 22nd, 2007, 08:30 PM
#3
Thread Starter
Lively Member
Re: If If If If
Thank you, you have been a great help.
Regards
Nathum
Evolution is just so unlikely to produce complex life forms.

-
Jun 23rd, 2007, 10:16 AM
#4
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)
-
Jun 23rd, 2007, 10:59 AM
#5
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.
-
Jun 30th, 2007, 04:15 AM
#6
Thread Starter
Lively Member
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
Evolution is just so unlikely to produce complex life forms.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|