Q: Modify a byte of a C# color int or uint 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.

A: Generally what I do is create a struct as you've said:
public struct Pixel {
    public byte Blue;
    public byte Green;
    public byte Red;
    public byte Alpha;
}

Then I have a utility functions such as:
public void SetPixel(int x, int y, Pixel colour) {
    Pixel* pixel = GetPixelAt(x, y);
    *pixel = colour;
}

public Pixel* GetPixelAt(int x, int y) {
    return (Pixel*)((byte*)_bitmapData.Scan0.ToPointer() + y * _width + x * sizeof(Pixel));
}

Then you can just call it like this:
Pixel p;
p.Red = 255;
p.Green = 0;
p.Blue = 0;
p.Alpha = 255;

SetPixel(0, 0, p);

_bitmapData is the BitmapData structure returned via LockBits.
EDIT:
From comments. This is how you would convert them:
// from pixel to int
Pixel p;
p.Red = 0;
p.Green = 0;
p.Blue = 0;
p.Alpha = 0;

int* i = (int*)&p;

// from int to pixel
Pixel p2;

p2 = *(Pixel*)&i;

Untested, but should be okay.

A: To use pointers you must use unsafe code. But you can achieve what you want using shift and bitwise operators.
// attempt to read individual bytes of an int
int r = (color & 0x00FF0000)>>16;
int g = (color & 0x0000FF00)>>8;
int b = (color & 0x000000FF);

Console.WriteLine("R-{0:X}, G-{1:X}, B-{2:X}", r, g, b);
// attempt to modify individual bytes of an int
int r1 = 0x1A;
int g1 = 0x2B;
int b1 = 0x3C;
color = (color & ~0x00FF0000) | r1 << 16;
color = (color & ~0x0000FF00) | g1 << 8;
color = (color & ~0x000000FF) | b1;

Console.WriteLine("Color-{0:X}", color);

You can wrap this snippet in structure as you wish.
This is solution with unsafe code, you must set allow unsafe code in your build options.
using System;

namespace PixelTest
{
    public unsafe struct Pixel
    {
        private int _color;

        public Pixel(int color)
        {
            _color = color;
        }

        public int GetColor()
        {
            return _color;
        }

        public int GetR()
        {
            fixed(int* c = &_color)
            {
                return *((byte*)c + 2);
            }
        }

        public int GetG()
        {
            fixed(int* c = &_color)
            {
                return *((byte*)c + 1);
            }
        }

        public int GetB()
        {
            fixed(int* c = &_color)
            {
                return *(byte*)c;
            }
        }

        public void SetR(byte red)
        {
            fixed (int* c = &_color)
            {
                *((byte*)c + 2) = red;
            }
        }

        public void SetG(byte green)
        {
            fixed (int* c = &_color)
            {
                *((byte*)c + 1) = green;
            }
        }

        public void SetB(byte blue)
        {
            fixed (int* c = &_color)
            {
                *(byte*)c = blue;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Pixel c = new Pixel(0xFFAABB);
            Console.WriteLine("R-{0:X}, G-{1:X}, B-{2:X}", c.GetR(), c.GetG(), c.GetB());
            c.SetR(0x1A);
            c.SetG(0x2B);
            c.SetB(0x3D);
            Console.WriteLine("Color - {0:X}", c.GetColor());
        }
    }
}

