我正在尝试将 Kinect 的视频流显示到 PictureBox 中。原因是,我想用一些图像覆盖它并使用 FillEllipse() 方法来添加实时标记。但是,我最终得到了一个带有红色 x(十字)的盒子。有人可以告诉我,我哪里出错了?我应该改用 WritableBitmap 吗?我想到了这一点,但是可写位图不提供诸如 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
}
}
非常感谢!
问候, 伊克尔