我正在尝试通过 C# 中的 FileStream 将从网络摄像头获取的图像写入文件。它们是 16 位单色的,所以我不能只写出 Bitmap 对象。我正在使用 Marshal.Copy() 来解决这个问题,如下所示:
unsafe private void RecordingFrame()
{
Bitmap bm16;
BitmapData bmd;
Emgu.CV.Image<Gray, UInt16> currentFrame;
const int ORIGIN_X = 0;
const int ORIGIN_Y = 0;
// get image here and put it in bm16...
bmd = bm16.LockBits(new Rectangle(ORIGIN_X, ORIGIN_Y, bm16.Width, bm16.Height),
ImageLockMode.ReadOnly, bm16.PixelFormat);
var length = bmd.Stride * bmd.Height;
byte[] bytes = new byte[length];
Marshal.Copy(bmd.Scan0, bytes, 0, length);
fsVideoWriter.Write(bytes, 0, length);
bm16.UnlockBits(bmd);
}
这是实现这一目标的最佳方法吗?我想简单地将 BitmapData 的 Scan0 成员作为指向 FileStream 的指针传递,但我不知道如何执行此操作,因此我将数据复制到字节缓冲区中。这会稍微降低性能,所以如果我可以改进它以实现更高的帧速率,我想这样做。