我有一个特殊的摄像机(使用 GigEVision 协议(protocol)),我使用提供的库进行控制。我可以订阅帧接收事件,然后通过 IntPtr 访问帧数据。
在我的旧 WinForms 应用程序中,我可以通过从数据创建位图对象并将其设置为 PictureBox 图像,或者通过将 PictureBox 句柄传递给所提供的库中的函数来渲染框架,该函数将直接在该区域上绘制.
在 WPF 中做类似事情的最好和最快的方法是什么?摄像机的运行速度在 30 到 100 fps 之间。
编辑(1):
由于帧接收事件不在 UI 线程上,因此它必须跨线程工作。
编辑(2):
我找到了使用 WriteableBitmap 的解决方案:
void camera_FrameReceived(IntPtr info, IntPtr frame)
{
if (VideoImageControlToUpdate == null)
{
throw new NullReferenceException("VideoImageControlToUpdate must be set before frames can be processed");
}
int width, height, size;
unsafe
{
BITMAPINFOHEADER* b = (BITMAPINFOHEADER*)info;
width = b->biWidth;
height = b->biHeight;
size = (int)b->biSizeImage;
}
if (height < 0) height = -height;
//Warp space-time
VideoImageControlToUpdate.Dispatcher.Invoke((Action)delegate {
try
{
if (VideoImageControlToUpdateSource == null)
{
VideoImageControlToUpdateSource =
new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, BitmapPalettes.Gray256);
}
else if (VideoImageControlToUpdateSource.PixelHeight != height ||
VideoImageControlToUpdateSource.PixelWidth != width)
{
VideoImageControlToUpdateSource =
new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, BitmapPalettes.Gray256);
}
VideoImageControlToUpdateSource.Lock();
VideoImageControlToUpdateSource.WritePixels(
new Int32Rect(0, 0, width, height),
frame,
size,
width);
VideoImageControlToUpdateSource.AddDirtyRect(new System.Windows.Int32Rect(0, 0, width, height));
VideoImageControlToUpdateSource.Unlock();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
});
}
在上面,VideoImageControlToUpdate
是一个 WPF 图像控件。
为了提高速度,我相信在 codeplex 上找到的 VideoRendererElement 更快。
最佳答案
最佳方式: WriteableBitmap.WritePixels(..., IntPtr source, ...)
最快的方法: 在 IntPtr 非托管内存中使用 WIC 和所有操作。但为什么在这种情况下完全使用 WPF?如果需要那种性能,请考虑使用 DirectX 覆盖。
关于c# - 如何从 WPF 中的原始帧渲染视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1232550/