I’m working with bitmaps in C#, and as you know bitmaps are usually stored as 24bpp or 32bpp in memory (when locked). After extracting the int color value of a pixel, I need to read one byte at a time to get R, G, B and edit the same bytes to modify RGB respectively. Is there a way using pointers to modify any given byte of a 4-byte 32-bit int? Or maybe I can create a struct and cast the int to a struct that allows access/modification of individual bytes?
int color = 0xFFAABB
// attempt to read individual bytes of an int
int R = *((*color));
int G = *((*color)+1);
int B = *((*color)+2);
// attempt to modify individual bytes of an int
*((*color)) = R;
*((*color)+1) = G;
*((*color)+2) = B;
I need such a method for fast reading/writing of individual bytes. If there is an unsafe method to work with such pointers I don’t mind that too. I cannot convert each pixel to a Color object since its too slow for high-speed image manipulation.
Generally what I do is create a struct as you’ve said:
Then I have a utility functions such as:
Then you can just call it like this:
_bitmapDatais theBitmapDatastructure returned viaLockBits.EDIT:
From comments. This is how you would convert them:
Untested, but should be okay.