Picturebox question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Omelskitot
    New Member
    • Aug 2012
    • 10

    Picturebox question

    id like to know if its possible. i need to find the location of black and white color in the picturebox. per pixel and store it to array. for example; there are color black from x1 to y5. i want to store it to array example; pixel[x1,y1],[x1,y2],[x1,y3],[x1,y4],[x1,y5] how can i do it automatically? Tnx in advance
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    The code posted in your later question, which converts a picture to grayscale, should be quite capable of examining each pixel and telling you whether it's black or white.

    Do you still need further help with this one?

    Comment

    • Omelskitot
      New Member
      • Aug 2012
      • 10

      #3
      yes sir i still need further help with this one?

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Not sure exactly what aspect of this you need help with.

        Most likely you're looking at a slight variation on your "gray scale" code. For example, let's assume you have a global or form-level two-dimensional array of type Boolean called ThisIsBlack().

        Code:
        Dim Color As Long
        Dim i As Long
        Dim j As Long
        Picture2.ScaleMode = vbPixels
        X = Picture2.ScaleWidth - 1
        Y = Picture2.ScaleHeight - 1
        ReDim ThisIsBlack (X, Y)
        For i = 0 To Y
          For j = 0 To X
            Color = Picture2.Point(j, i)
            If Color = 0& Then
              ThisIsBlack (j, i) = True
            End If
          Next
        Next
        This is just quickly typed here so may need some debugging. but that should populate your array to show which pixels are black.

        Comment

        • Omelskitot
          New Member
          • Aug 2012
          • 10

          #5
          where can i view the output? on a textbox?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            That's kind of a tough one to answer. The sample code I provided doesn't produce any output, except for the values in the array.

            To see these values, you'd have to display them in some way. For example you might place them in a textbox, show them on another picturebox (Pset), print them to the output window (Debug.Print) or in some other way.

            Comment

            • Omelskitot
              New Member
              • Aug 2012
              • 10

              #7
              I saw this code made by PsychoCoder
              Code:
                  Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
                   
                  Private Sub Form_Load()
                      pixBox.Picture = LoadPicture("YourPicturePath")
                      Dim i As Long, j As Long, nBlack As Long, nWhite As Long, pHdc As Long, tempColor As Long
                      Show
                      With pixBox
                          pHdc = .hdc
                          .ScaleMode = 3
                          For i = 0 To .ScaleWidth
                              For j = 0 To .ScaleHeight
                                  tempColor = GetPixel(pHdc, i, j)
                                  If tempColor = vbWhite Then
                                      nWhite = nWhite + 1
                                  ElseIf tempColor = vbBlack Then
                                      nBlack = nBlack + 1
                                  End If
                              Next
                          Next
                      End With
                      MsgBox "White : " & nWhite & vbCrLf & "Black : " & nBlack, vbInformation
                  End Sub
              Close enough on what im planning to do. but this one count all the black and white how can manipulate this that it can output the pixel(x,y) of the black and white? Thanks

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Well, instead of adding up a counter for black or white, you could print out the coordinates (in this case, i and j) and "black" or "white". It all depends on what you want to do with this output. Is it just for testing so you can check the code is working?

                Comment

                • Omelskitot
                  New Member
                  • Aug 2012
                  • 10

                  #9
                  Here's the example the output show the total number of black and white, Now my question is how can i see each pixel of the color black. in the picture x axis i assume the number is the last pixel for white and the y axis for the last pixel for black

                  Comment

                  • Omelskitot
                    New Member
                    • Aug 2012
                    • 10

                    #10
                    i add text1.text for the i and text2.text for the j
                    Code:
                     With Picture2
                                pHdc = .hdc
                                .ScaleMode = 3
                                For i = 0 To .ScaleWidth
                                    For j = 0 To .ScaleHeight
                                        tempColor = GetPixel(pHdc, i, j)
                                     
                                        If tempColor = vbWhite Then
                                            nWhite = nWhite + 1
                                            Text1.Text = i
                                            
                                        ElseIf tempColor = vbBlack Then
                                            nBlack = nBlack + 1
                                            Text2.Text = j

                    Comment

                    Working...