I have a non transparent, colour bitmap with length 2480 and width 3507.
Using Bitmap.GetPixel(int x, int y) I am able to get the colour information of each pixel in the bitmap.
If I squirt the bitmap into a byte[]:
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
ms.Position = 0;
byte[] bytes = ms.ToArray();
then I’d expect to have the same information, i.e. I can go to bytes[1000] and read the colour information for that pixel.
It turns out that my array of bytes is larger than I anticipated. I thought I’d get an array with 2480 x 3507 = 8697360 elements. Instead I get an array with 8698438 elements – some sort of header I presume.
In what format the bytes in my array stored? Is there a header 1078 bytes long followed by Alpha, Red, Green, Blue values for every byte element, or something else?
All I need is the colour information for each pixel. I’m not concerned with the header (or indeed the transparency) unless I need it to get the colour info.
You’re calling
GetBufferwhich returns the underlying byte array – that’s bigger than the actual length of the stream.Either use
or use
GetBufferbut in conjunction withms.Length.Having said that, you’re saving it as a BMP – so there’ll be header information as well; it’s not like the first byte will represent the first pixel. Unfortunately there’s no “raw” image format as far as I can see… that’s what it sounds like you really want.
You could use
Bitmap.LockBitsand then copy the data from there, if you want…