步幅的另一个问题

Yet another issue with stride

According to the documentation 从 byteArray 创建位图的步幅需要为:

The stride is the width of a single row of pixels (a scan line), rounded up to a four-byte boundary. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.

我有一个 byteArray 用于 640 x 512 像素的图像。字节数组是从来自实时摄像机的数组缓冲区创建的。我是第一次创建图像。图像格式为 PixelFormat.Format24bppRgb,因此每个红色、绿色和蓝色一个字节,每个像素总共三个字节。这使得图像的一行为 640 * 3 字节 = 1,920 字节。检查这是否可以被四整除我得到 1,920/4 = 480.

当我使用 640 * 3 = 1,920 的步幅时,我得到了一个糟糕的结果(乱码图像)。我做错了什么?

我已验证我的 byteArray 具有正确数量的元素并且看起来正确。我的代码如下所示:

int stride = 1920;
using (Bitmap image = new Bitmap(640, 512, stride, PixelFormat.Format24bppRgb, new IntPtr(ptr)))
{
    return (Bitmap)image.Clone();
}

编辑 听起来 stride at 1920 是正确的,你们都在确认我理解 stride 是如何工作的。我现在的猜测是我的像素布局(ARGB VS RGB 或类似的东西)不正确。

图像应该是这样的(使用免费的 GenIcam 查看器:

"garbled" 图片的外观:

已解决!

了解步幅的工作原理是关键。由于我的步伐正确,所以我开始寻找其他地方。发生的事情是我传递了一个错误值的数组来制作位图。这些值应具有温度值:

23.5, 25.2, 29.8 ...

实际通过的值在千位范围内... 7501、7568、7592 ...

垃圾进,垃圾出...

漫无边际的想法: 在做这样的事情时,我需要学习如何设置单元测试。当您测试诸如计算圆周长的方法时,单元测试很容易。我想我需要想出一种方法来设置原始数据文件,该文件模拟来自相机的原始数据,然后 运行 通过所有转换得到图像。

感谢您的帮助。