C# - Remove Bitmap padding

前端 未结 3 1850
花落未央
花落未央 2021-01-15 23:13

I was wondering if there\'s a way in order to remove the padding generated by the 24 bit Bitmap for each scan line.

What I mean is like this :

Original [Pure

3条回答
  •  -上瘾入骨i
    2021-01-15 23:52

    No, you'll have to remove it yourself. Padding is added to ensure that the start of a scanline in the bitmap starts at a multiple of 4. So getting padding is pretty likely when the pixel format is 24bpp, bmp_bitmap.Width * 3 is only divisible by 4 by accident.

    You'll need a loop to copy each line. Something like this:

    byte[] bytes = new byte[bmpData.Width * bmpData.Height * 3];
    for (int y = 0; y < bmpData.Height; ++y) {
        IntPtr mem = (IntPtr)((long)bmpData.Scan0 + y * bmpData.Stride);
        Marshal.Copy(mem, bytes, y * bmpData.Width * 3, bmpData.Width * 3);
    }
    

提交回复
热议问题