locked
When using .ToArgb I get negative numbers? RRS feed

  • Question

  • I'm trying to find the colors of pixels using GetPixel(x,y) with bitmaps.

    I understand how to use it and it works.

    When I use .ToString I get the string version of the color.

     

    Now when I use .ToArgb I get a negative number and the colors are backwards?

    16777215 should be white and 0 should be black.

     

    This is an example of some results:

    -16273900 is showing close to black and -32567 is showing close to white.

     

    This is backwards??? And why negative numbers???

    Tuesday, October 12, 2010 12:56 AM

Answers


  • Sorry, I see this now but I had answered your other thread.  Here is what I said there:
     
    Because ToArgb returns an integer.  The highest byte of this integer is the alpha (transparency) channel.  In many cases, the alpha value will make the integers sign bit be negative.
     
    What kind of representation do you want for the color of a pixel?

    --
    Mike
    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:39 PM
    Tuesday, October 12, 2010 1:16 AM
  • You will need to access the A R G and B properties of the color structure separately.   If you need the color information merged in some way you will need to use those separate values and combine them according to particular format for the color information that you need to use.

     

    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:39 PM
    Tuesday, October 12, 2010 1:48 AM
  • As Mike pointed out, the ARGB value includes four bytes with the first being the alpha value and the other three being the RGB (red, blue, green) values. The examples you give where 16777215 is white and 0 is black are just the RGB values (or the ARGB values when A is zero). Any time the A (alpha) value is 128 or higher, the ARGB value is going to be a negative integer.

    If, for some reason, you need an integer that represents just the RGB value, you could use :

    Dim myColor = myBitMap.GetPixel(x, y)
    Dim myRGB as Integer = myColor.R * 256 * 256 + myColor.G * 256 + myColor.B
    

    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:42 PM
    Tuesday, October 12, 2010 2:01 AM
  • I didn't know the alpha number would show as a negative number over 128. That was throwing me off.

    I've got it taken care of now, Thanks.

    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:42 PM
    Tuesday, October 12, 2010 12:42 PM

All replies


  • Sorry, I see this now but I had answered your other thread.  Here is what I said there:
     
    Because ToArgb returns an integer.  The highest byte of this integer is the alpha (transparency) channel.  In many cases, the alpha value will make the integers sign bit be negative.
     
    What kind of representation do you want for the color of a pixel?

    --
    Mike
    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:39 PM
    Tuesday, October 12, 2010 1:16 AM
  • You will need to access the A R G and B properties of the color structure separately.   If you need the color information merged in some way you will need to use those separate values and combine them according to particular format for the color information that you need to use.

     

    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:39 PM
    Tuesday, October 12, 2010 1:48 AM
  • As Mike pointed out, the ARGB value includes four bytes with the first being the alpha value and the other three being the RGB (red, blue, green) values. The examples you give where 16777215 is white and 0 is black are just the RGB values (or the ARGB values when A is zero). Any time the A (alpha) value is 128 or higher, the ARGB value is going to be a negative integer.

    If, for some reason, you need an integer that represents just the RGB value, you could use :

    Dim myColor = myBitMap.GetPixel(x, y)
    Dim myRGB as Integer = myColor.R * 256 * 256 + myColor.G * 256 + myColor.B
    

    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:42 PM
    Tuesday, October 12, 2010 2:01 AM
  • I didn't know the alpha number would show as a negative number over 128. That was throwing me off.

    I've got it taken care of now, Thanks.

    • Marked as answer by Lord Odyssey Tuesday, October 12, 2010 12:42 PM
    Tuesday, October 12, 2010 12:42 PM