I am working with some image processing code in C#. Because performance is critical, I'm doing this in unsafe code with pointers.
Here's some code to precede my question:
Rectangle Image_Rectangle = new Rectangle(0, 0, MyImage.Width, MyImage.Height);
BitmapData Image_Data = MyImage.LockBits(Image_Rectangle, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
// ... x and y nested for-loops to work with each pixel
Byte* PixelRow = (Byte*)Image_Data.Scan0 + (y * Image_Data.Stride);
Once I have the above Byte pointer, I can set its values like so:
PixelRow[(x * 3) + 2] = 255;
PixelRow[(x * 3) + 1] = 255;
PixelRow[(x * 3)] = 255;
However, I would prefer to access these as an array:
Byte[] RGB = { PixelRow[(x * PIXEL_SIZE) + 2], PixelRow[(x * PIXEL_SIZE) + 1], PixelRow[(x * PIXEL_SIZE) + 0] };
RGB[0] = 255;
RGB[1] = 255;
RGB[2] = 255;
The problem is when I try to assign the value, I sense I'm not working with the actual pointer anymore. Once I unlock bits, the resulting bitmap is unchanged (when using the array method).
I'm pretty new to pointers, can anyone help explain what's going on and how to properly maintain pointers through the use of an array?