繁体   English   中英

如何在Windows.Forms.PictureBox中正确显示Kinect视频流?

[英]How can I show Kinect video stream in Windows.Forms.PictureBox appropriately?

我正在尝试将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
    }
}

非常感谢你!

此致,ikel

我找到了答案。 处置是必要的指示,以腾出资源在这里 问题是,我在绘制之后处理得太早,所以看起来好像没有画出来。 但是,无论如何,我在这里给出了更明确的回答。

Bitmap继承自Image ,它实现了IDisposable ,所以当你完成使用实例时,你应该在它上面调用Dispose() 这将清除Image中的非托管资源。

但是, Image 还实现finalizer ,因此如果由于某种原因您无法调用Dispose() ,则在实例的最终确定期间将回收资源,这将在实例不再被引用后的某个时刻发生。

我只是删除了kinectVideoBitmap.Dispose(); 我的Windows窗体在PictureBox控件中显示Kinect的视频流。

此致,ikel

我不清楚为什么你使用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>

第二个画布处于较高的z-index,其上的任何对象都将覆盖您的视频流。

PS尽管我的代码指向深度查看器,但在使用SDK中的示例时,视频流的执行方式相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM