Bitmap Stride and ratio of 4 bytes?

What does this sentence mean:

The Stride property contains the width of one line in bytes. The size of the line, however, may not be an exact multiple of the size of the pixel, because for efficiency the system ensures that the data is packed into lines that start at the border of four bytes and are padded to four bytes.

+8
c # image image-processing bitmap
source share
3 answers

Washer supplemented. This means that it is rounded to the nearest multiple of 4. (assuming 8 bits are gray or 8 bits per pixel):

Width | stride -------------- 1 | 4 2 | 4 3 | 4 4 | 4 5 | 8 6 | 8 7 | 8 8 | 8 9 | 12 10 | 12 11 | 12 12 | 12 

and etc.

In C #, you can implement this as follows:

 static int PaddedRowWidth(int bitsPerPixel, int w, int padToNBytes) { if (padToNBytes == 0) throw new ArgumentOutOfRangeException("padToNBytes", "pad value must be greater than 0."); int padBits = 8* padToNBytes; return ((w * bitsPerPixel + (padBits-1)) / padBits) * padToNBytes; } static int RowStride(int bitsPerPixel, int width) { return PaddedRowWidth(bitsPerPixel, width, 4); } 
+5
source share

This means that if the width of your image is 17 pixels and with 3 bytes for color, you will get 51 bytes. Thus, the width of your image in bytes is 51 bytes, and the step is 52 bytes, that is, the width of the image in bytes, rounded to the next 4-byte border.

+8
source share

Let me give an example:

This means that if the width is 160, the step will be 160. But if the width is 161, then the step will be 164.

+4
source share

All Articles