I'm trying to copy a simple byte array to an 8bit indexed bitmap. Using the exact same code as shown in countless answered questions on many forums, I still get the wrong result. The data I'm trying to write to image files is 360 bytes, setup as an 18x20 byte linear array. That is, the first 18 bytes (0-17) belong on the top row of the image, the next 18 bytes (18-35) belong on the 2nd row, etc. I have confirmed that this data is correct, as I can manually parse it in Excel (and even visualize it by setting the background color of cells). However, when I try to extract this using code in c#, I get a wrongly formatted image. Here is the code...
public Bitmap CopyByteDataToBitmap(byte[] byteData) {
Bitmap bmp = new Bitmap(18, 20, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(byteData, 0, bmpData.Scan0, byteData.Length);
bmp.UnlockBits(bmpData);
return bmp;
}
The result is as follows. The first row is written correctly. However, the starting with the second row, there is a 2 byte offset. That is, the first byte of the second row of the image ends up being byte #20 instead of byte #18 (starting from 0). Also, if I set a breakpoint immediately after the LockBits call, I can see that the bmpData has a "Stride" property equal to 20... even though the width is clearly set to 18. And, if I manually set the stride to 18, after the LockBits, it has no effect on the returned bitmap. Why is this happening? Please help, thanks.
You have to copy it row by row, advancing your read position by the stride used in your image data, and your write position by the stride set in the
BitmapData
object.In your case, the input data's stride is just the width, but the
BitmapData's
stride won't match that since, as TaW said, it's always rounded up to the next multiple of 4 bytes.Also note, it being an 8-bit image, you need to add the palette, or it'll end up with a standard Windows palette that probably won't match your image's intended colours at all.