Read RGB value from a bitmap 256 color

Ed_N
09-22-2006, 09:43 AM
Hi

If I want to read the color value from a 256 bitmap picture, how do I do it?
At the moment I'm doing it this way:

Dim clr As color = bitmap.GetPixel(x, y)

but I get something like this:

A:255 R:128 G:128 B:128

What I'm looking for is for a RGB 8 bit value, that is:

D0, D1, D2 = 3 bit for Red
D3, D4, D5 = 3 bit for Green
D6, D7 = 2 bit for Blue

then I can have a value like 0xF9 which would be a live red.
Anyone know how to do this?

OnErr0r
09-22-2006, 10:19 AM
If you have a 256 color bitmap (8 bit), then it can have 256 24bit colors. I'm normally using LockBits when I get pixel values, but you can use GetPixel too, it's just much slower. From what you're saying, GetPixel must be returning indexes into the color table. If that's the case, you'll either have to get at the color table, or switch to lockbits.

I'm not aware of GDI+ supporting a 3-3-2 8 bit bitmap.

Ed_N
09-22-2006, 10:45 AM
So for what I understand, even if I get the byte value, it won't be a RGB8?

I'm confused now. If it has only 256 colors, then you need 1 byte for the color value.

Could you please post a small example of how to read the values using lockbit? Say I have a bitmap of 4 x 4 and I want the to know the byte value of the pixel at 1,1

I noticed that if I open a bitmap file and see it with a Hex editor, the values I see correspond to the byte values I'm lookin for.

OnErr0r
09-22-2006, 11:17 AM
So for what I understand, even if I get the byte value, it won't be a RGB8?

If it has only 256 colors, then you need 1 byte for the color value.

That is correct. The value of the byte being 0 to 255, which corresponds to the index in the color table of the bitmap.

You'll find LockBits code here: http://www.xtremevbtalk.com/showthread.php?t=128891

OnErr0r
09-22-2006, 11:23 AM
Btw, if you call LockBits with Imaging.PixelFormat.Format32bppArgb, then you'll have the R, G and B you want, rather than color table indexes. 32 bit BMPs are an array of pixel data from top to bottom. The nice thing about them is there is no padding per scanline (horizontal line of pixels), so you don't have to deal with that.

Ed_N
09-25-2006, 09:50 AM
This is what I was looking for:


Dim photo() as byte
Photo = My.Computer.FileSystem.ReadAllBytes(ImageFilePath)


Now by knowing the number of pixels the photo has, I can read them from the end backwards (the bmp file has more than the pixels value).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum