简体   繁体   中英

Why does this bitmap-manipulation code subtract the width from the stride?

What does the line imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4); mean in the following code?

var width = bitmap.Width;
var height = bitmap.Width;

var horizontalProjection = new double[width];
var verticalProjection = new double[height];

var bitmapData1 = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

        unsafe
        {
            var imagePointer1 = (byte*)bitmapData1.Scan0;

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    var blu = imagePointer1[0];
                    var green = imagePointer1[1];
                    var red = imagePointer1[2];

                    int luminosity = (byte)(((0.2126 * red) + (0.7152 * green)) + (0.0722 * blu));

                    horizontalProjection[x] += luminosity;
                    verticalProjection[y] += luminosity;

                    imagePointer1 += 4;
                } 

               imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
            }
        }

        MaximizeScale(ref horizontalProjection, height);
        MaximizeScale(ref verticalProjection, width);

        var projections = 
            new[]
                {
                    horizontalProjection, 
                    verticalProjection
                };

        bitmap.UnlockBits(bitmapData1);
        return projections;

The code is simply incrementing the pointer contained in the variable imagePointer1 .

The reason you have to increment by the stride - (width * 4) is because of the way bitmaps are represented in memory.

There's a very comprehensive explanation available as part of Bob Powell's GDI+ FAQ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM