我正在尝试将 Kinect 的视频流显示到 PictureBox 中。原因是,我想用一些图像覆盖它并使用 FillEllipse() 方法添加实时标记。 然而,我最终得到了一个盒子,里面有一个红色的x(十字)。有人可以告诉我,我哪里出了问题吗?我应该使用 WritableBitmap 代替吗?我有这个想法,但是 Writeable 位图没有提供诸如 FillEllipse() 之类的方法来放置标记。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Kinect;
using System.Drawing.Imaging;
using System.Drawing;
using System.Runtime.InteropServices;
namespace fTrack_WF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
KinectSensor myKinect;
private void Window_Loaded(object sender, EventArgs e)
{
if (KinectSensor.KinectSensors.Count == 0)
{
MessageBox.Show("No Kinects device detected", "Camera View");
Application.Exit();
return;
}
try
{
// get first Kinect device attached on computer
myKinect = KinectSensor.KinectSensors[0];
// enable depth stream
myKinect.DepthStream.Enable();
// enable color video stream
myKinect.ColorStream.Enable();
// start the sensor
myKinect.Start();
// connect up the video event handler
myKinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(myKinect_ColorFrameReady);
}
catch
{
MessageBox.Show("Kinect initialise failed", "Camera viewer");
Application.Exit();
}
}
#region Video Image Processing
byte[] colorData = null;
Bitmap kinectVideoBitmap = null;
IntPtr colorPtr;
void myKinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
{
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null) return;
if (colorData == null)
colorData = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(colorData);
Marshal.FreeHGlobal(colorPtr);
colorPtr = Marshal.AllocHGlobal(colorData.Length);
Marshal.Copy(colorData, 0, colorPtr, colorData.Length);
kinectVideoBitmap = new Bitmap(
colorFrame.Width,
colorFrame.Height,
colorFrame.Width * colorFrame.BytesPerPixel;
PixelFormat.Format32bppRgb,
colorPtr);
kinectVideoBox.Image = kinectVideoBitmap;
kinectVideoBitmap.Dispose();
}
}
#endregion
}
}
非常感谢!
问候, 伊克尔
最佳答案
我不清楚为什么您使用 WinForms PictureBox 而不是仅使用 WPF。
您是否尝试过将 Canvas 放置在视频流之上(在 SDK 示例中进行了演示),并简单地对其进行了绘制?
<Grid HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="320" Height="240">
<lt:KinectDepthViewer x:Name="DepthViewer" KinectSensorManager="{Binding KinectSensorManager}" />
<Canvas>
<lt:KinectSkeletonViewer
KinectSensorManager="{Binding KinectSensorManager}"
Width="{Binding ElementName=DepthViewer, Path=ActualWidth}"
Height="{Binding ElementName=DepthViewer, Path=ActualHeight}"
ShowBones="True" ShowJoints="True" ShowCenter="True" ImageType="Depth" />
</Canvas>
</Grid>
<Canvas Name="DrawingCanvas">
</Canvas>
第二个 Canvas 的 z-index 较高,其上的任何对象都会覆盖您的视频流。
附注 虽然我的代码指向深度查看器,但使用 SDK 中的示例时,视频流的完成方式相同。
关于c# - 如何在 Windows.Forms.PictureBox 中正确显示 Kinect 视频流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12577935/