Results 1 to 7 of 7

Thread: [RESOLVED] If pixel equals X...help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Resolved [RESOLVED] If pixel equals X...help

    I'm using the MyBitmap.GetPixel(x, y) method. However it returns a value in as a color. I need to see if that color equals a predefined color. My issue is I want to code in that color myself, not compare 2 pixels. how do i see if the pixel is equal to [255,255,255] (which is white)? whenever i use Color.White it doesnt work!


    also is there a way to have VB change a 24Bit image into as a 8 bit gray scale? I'm highly new to the .NET world, (incase you havent noticed )

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: If pixel equals X...help

    Define "doesn't work".

    Show us your code.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: If pixel equals X...help

    When i run this code, it should stop, and give me all points in the image that are black [0,0,0] in color. However it runs though the entire loop never finding a "black" pixel. The image is a black background with a single white dot (approx 20 pixels in dia.) made in MS Paint. It's a 24bpp image.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim xx As Integer = 0
            Dim yy As Integer = 0
            Dim i As Integer = 0
            ' Create a Bitmap object from an image file.
            Dim myBitmap As New Bitmap("c:\lighttest.bmp")
            Dim pixelcolor As Color
    
            ' Get the color of a pixel within myBitmap.
            Label1.Text = TimeOfDay()
            Application.DoEvents()
            'For i = 1 To 100
    
            For xx = 1 To 200 Step 4
                Application.DoEvents()
                For yy = 1 To 200 Step 4
                    pixelcolor = myBitmap.GetPixel(xx, yy)
                    Label1.ForeColor = pixelcolor
                    Application.DoEvents()
                    If pixelcolor = Color.FromName("black") Then
                        MsgBox(xx & ", " & yy)
                    End If
                Next
            Next
            'Next
            MsgBox(TimeOfDay())
        End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: If pixel equals X...help

    I got it... I needed to use "Color.FromArgb(0, 0, 0)" thanks tho!

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] If pixel equals X...help

    What's wrong with Color.Black ?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: [RESOLVED] If pixel equals X...help

    Quote Originally Posted by penagate
    What's wrong with Color.Black ?
    Thats what i tried in the beginning. My image had a black background with a single white dot in the middle. I ran... If Bitmap.Getpixel = color.black then end ... and it would just run through without finding anything. I wonder if it is something to do with using a 24bbp image or something. Either way i solved it by using color.fromargb().

  7. #7
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [RESOLVED] If pixel equals X...help

    I also had problems directly comparing a known pixel color to predefined color even though their rgb and Argb values were the same.

    For 'normal' and particularly for user defined colors (which are nameless) you can also use the argb value for comparison - it is only one integer instead of 3+1.
    Code:
    '-16777216 is black, -1 is white etc.
    If myBitmap.GetPixel(xx, yy).ToArgb <> Color.Black.ToArgb  Then 
    
        MsgBox(xx & ", " & yy)
                    
    End If
    For your second question try this link.
    VB 2005, Win Xp Pro sp2

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