You ignore the step.
Lines of images can be supplemented with additional bytes on the left so that their size is divided by a number, for example (1 = no fill, 2, 4, 8 = default for many images, 16, ...).
In addition, the images can be the rectangle area in the large image, which makes βspacingβ between the lines in the smaller image even larger (since the step is a larger image step). - In this case, the image may also have an offset for the starting point in the buffer.
Best practice:
// Overload this method 3 time for different bit per SUB-pixel values (8, 16, or 32) // = (byte, int, float) // SUB-pixel != pixel (= 1 3 or 4 sub-pixels (grey or RGB or BGR or BGRA or RGBA or ARGB or ABGR) unsafe { byte[] buffer = image.Buffer; int stride = image.buffer.Length / image.PixelHeight; // or int stride = image.LineSize; (or something like that) fixed (float* regionStart = (float*)(void*)buffer) // or byte* or int* depending on datatype { for (int y = 0; y < height; y++) // height in pixels { // float* and float or byte* and byte or int* and int float* currentPos = regionStart + offset / SizeOf(float) + stride / SizeOf(float) * y; for (int x = 0; x < width; x++) // width in pixels { for (int chan = 0; chan < channel; chan++) // 1, 3 or 4 channels { // DO NOT USE DECIMAL - you want accurate image values // with best performance - primative types // not a .NET complex type used for nice looking values for users eg 12.34 // instead use actual sub pixel type (float/int/byte) or double instead! var currentValue = value; currentPos++; } } } } }
source share