我正在尝试将相机元数据复制到位图中,并且因为元数据中的每个值都是 16 位(或 ushort),所以我认为将其显示在 16bpp garyscale 位图中是明智的。我写的代码如下:
// Getting the metadata from the device
metaData = new DepthMetaData();
dataSource.GetMetaData(metaData);
// Setting up bitmap, rect and data to use pointer
Bitmap bitmap = new Bitmap(metaData.XRes, metaData.YRes, PixelFormat.Format16bppGrayScale);
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData data = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format16bppGrayScale);
// Pointer pointing to metadata
ushort* ptrMetaData = (ushort*)dataSource.DepthMapPtr.ToPointer();
lock(this)
{
// Runs through the whole bitmap and assigns the entry in the metadata
// to a pixel
for (int y = 0; y < bitmap.Height; ++y)
{
ushort* ptrDestination = (ushort*)data.Scan0.ToPointer() + y * data.Stride;
for (int x = 0; x < bitmap.Width; ++x, ++ptrMetaData)
{
ptrDestination[x] = (ushort)*ptrMetaData;
}
}
}
// Once done unlock the bitmap so that it can be read again
bitmap.UnlockBits(data);
运行元数据的 XRes = 640 和 YRes = 480 时。代码在“ptrDestination[x] = (ushort)*ptrMetaData;”的 for 循环中引发内存访问异常 只运行了 240 行,占总数的一半。
我将它与 8bpp 一起使用,在那里我降低了分辨率并且效果很好,所以我不明白为什么它不应该在这里。也许有人发现了问题。
已经谢谢了