Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello all

I have 24 bitmap image, and i have to convert it to byte array to save it to binary file.

I cant convert my 24 bitmap to 32 bitmap for some reasons, and the problem is:

The ordering of pixels is blue, green, red and (not used) within the 32 bpp result. The ordering within the BIN file is resin, red, green and blue. So my code have to flip the ordering of those bytes when writing the BIN file.

So the ordering of the Header of the binary array for this image have to be (Red, Green, blue) instead of (Blue, Green, Red) and copy my image in it without change the header that replaced to (Red, Green, Blue).

Any idea to do this by C#?

Thank you.
Posted

1 solution

You can use bit shifting, masking, and ORing to extract bytes and then recombine them in a different order.
C#
rgbu = ((bgr & 255) << 24) | ((bgr & (255 << 8)) << 16) | ((bgr & (255 << 16)) << 8);

You might have to cast some of those as unsigned integers to ensure you don't encounter any negative funkiness. Also, I wasn't sure what the source and destination format were, so I just assumed you were converting from blue/green/red to red/green/blue/unused (most significant byte to least significant byte).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900