0

I read this link Converting 8 bit color into RGB value

Then I tried the VB.NET code like the following:

Private Sub picturebox1_MouseDown(ByVal sender As Object, _
                          ByVal e As System.Windows.Forms.MouseEventArgs) _
                          Handles picturebox1.MouseDown

    Dim bm As New Bitmap(picturebox1.Image)
    Dim Red As Byte = bm.GetPixel(e.X, e.Y).R
    Dim Green As Byte = bm.GetPixel(e.X, e.Y).G
    Dim Blue As Byte = bm.GetPixel(e.X, e.Y).B

    Dim ColorNumber As Byte = ((Red / 32) << 5) + ((Green / 32) << 2) + (Blue / 64)

    ' Show Byte Number of color
    MsgBox(ColorNumber)

    MsgBox(Red & ":" & Green & ":" & Blue)

    Red = (ColorNumber >> 5) * 32
    Green = ((ColorNumber >> 2) << 3) * 32
    Blue = (ColorNumber << 6) * 64

    MsgBox(Red & ":" & Green & ":" & Blue)


End Sub

But when one pixel is selected, an error occurs:

Arithmetic operation resulted in an overflow.

How do I get a byte value of an image with 256 colors (8 bits), and then restore the (conversion) resulting byte value into the RGB value.

Thanks :)

Community
  • 1
  • 1
Tomero Indonesia
  • 1,685
  • 2
  • 16
  • 17
  • 1
    You have more than 8 bits of color. – SLaks Aug 08 '13 at 13:30
  • 256 color images are usually stored using a palette. In other words, the 8 bit color value is not a combination of RGB, but just a palette index to look up the actual RGB. 8 bits alone is not enough to store RGB. That would only allow 2 bits per color, which would not be nearly enough. The acutal RGB value stored in the palette would be larger than 8 bits. – Steven Doggart Aug 08 '13 at 14:00
  • @SLaks I use images with 256 color BMP format. I edit using microsoft paint. – Tomero Indonesia Aug 08 '13 at 23:07
  • @StevenDoggart Is there any example in VB.NET? thanks – Tomero Indonesia Aug 08 '13 at 23:08
  • @user2577020 An example of what? Reading the colors in the palette? I'm not sure what you are asking. – Steven Doggart Aug 08 '13 at 23:20
  • @StevenDoggart thanks for your answer. I just want to read the color on picturebox in byte format (0-255) and not RGB. And how to set color on picturebox using byte values ​​(0-255) / 256 color? – Tomero Indonesia Aug 08 '13 at 23:35

1 Answers1

1

Your ColorNumber has been declared as a Byte, which can only store values from 0 to 255... Change the code to this:

Dim ColorNumber As Int32 = ((Red / 32) << 5) + ((Green / 32) << 2) + (Blue / 64)

Also, since you're using .Net you can just get the color with this function:

Dim color As Color = Color.FromRgb(Red, Green, Blue)
bastos.sergio
  • 6,684
  • 4
  • 26
  • 36
  • That's what I'm confused, 256 colors should be 0 to 255 or equal to 1 byte or 8 bits. I want a sample code to retrieve the color value in bytes and can be applied back in the picture. any solution? – Tomero Indonesia Aug 08 '13 at 23:22
  • 1
    Color information can't be stored as a byte... Yes, 256 colors can be stored as the values 0 to 255, but the values will be just indexes to another struture that contains the real color information (i.e. the palette.) Please check out [this site](http://paulbourke.net/dataformats/bitmaps/) for more information. – bastos.sergio Aug 09 '13 at 14:00