While trying to better understand all the properties of Bitmap and locked bits, I created a 10x1px image, with the following properties:
Pixel format:Format24bppRgb
Specifies that the format is 24 bits per pixel; 8 bits each are used for the red, green, and blue components.
Color depth in bits: 24 (3 bytes)
So that means I got 10 pixels that each need 3 bytes which means I need to create a byte array of length 30 to copy in all the image data using:
Marshal.Copy(lockedBitmapData.Scan0, bytesArray, 0, 30);
The stride is the width of a single row of pixels (a scan line), rounded up to a four-byte boundary.
Does that mean that when I access BitmapData.Stride
, it will always return an integer as if the PixelFormat was one of the 32bit
variations?
Why the code below causes 2 additional bytes that are 0, instead of 10 that are 0? Or is it 2 because it is rounding up 30 bytes to 32 as the closest byte length that is divisible by 4 as the doc says? I think I misunderstood documentation and expected it to count 4 bytes for every pixel when calculating stride.
byte[] bytesArray = new byte[lockedBitmapData.Stride];
Marshal.Copy(lockedBitmapData.Scan0, bytesArray, 0, lockedBitmapData.Stride);