Results 1 to 6 of 6

Thread: If If If If

  1. #1

    Thread Starter
    Lively Member ADF-Sniper's Avatar
    Join Date
    Sep 2005
    Location
    Melbourne, Australia
    Posts
    92

    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.


  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  3. #3

    Thread Starter
    Lively Member ADF-Sniper's Avatar
    Join Date
    Sep 2005
    Location
    Melbourne, Australia
    Posts
    92

    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.


  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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)

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  6. #6

    Thread Starter
    Lively Member ADF-Sniper's Avatar
    Join Date
    Sep 2005
    Location
    Melbourne, Australia
    Posts
    92

    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
  •  



Click Here to Expand Forum to Full Width