为什么这个位图操作代码要从步幅中减去宽度?

发布于 2024-11-01 17:02:36 字数 1495 浏览 1 评论 0原文

以下代码中的行 imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4); 是什么意思?

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2024-11-08 17:02:36

该代码只是递增变量imagePointer1 中包含的指针。

必须按步幅递增 - (宽度 * 4) 的原因是位图在内存中的表示方式。

Bob Powell 的 GDI+ 提供了非常全面的解释常见问题解答

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文