Bitmap LockBits results in wrong stride!

  • Thread starter Thread starter AEagle
  • Start date Start date
A

AEagle

Guest
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 is has a "Stride" property = 20... even though the width is clearly set to 18. Why is this happening? Please help.

Continue reading...
 
Back
Top