C# ColorBitmap被其他子流程使用

C# ColorBitmap被其他子流程使用,c#,bitmap,subprocess,kinect,C#,Bitmap,Subprocess,Kinect,我使用的是C#,我的代码如下。我知道这不是很优化,但我只需要帮助解决这个问题 internal class ImageProcessing { private KinectSensor kinectSensor = null; private ColorFrameReader colorFrameReader = null; private WriteableBitmap colorBitmap = null; private Mat frame = null;

我使用的是C#,我的代码如下。我知道这不是很优化,但我只需要帮助解决这个问题

internal class ImageProcessing
{
    private KinectSensor kinectSensor = null;
    private ColorFrameReader colorFrameReader = null;
    private WriteableBitmap colorBitmap = null;
    private Mat frame = null;
    private int verify = 0;
    public Mat ObjectRec(Mat Object)
    {
        long i;
        ObjectRecog();
        Thread.Sleep(2000);
        ImageSource();

            return ImageProcessingExt.RecogObject.Draw(Object, this.frame, out i);

    }
    public void ObjectRecog()
    {
        // get the kinectSensor object
        this.kinectSensor = KinectSensor.GetDefault();

        // open the reader for the color frames
        this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader();

        // wire handler for frame arrival
        this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived;

        // create the colorFrameDescription from the ColorFrameSource using Bgra format
        FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);

        // create the bitmap to display
        this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);

        // set IsAvailableChanged event notifier
        this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged;

        // open the sensor
        this.kinectSensor.Open();

    }

    private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)

        {
                // ColorFrame is IDisposable
                using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
                {
                    if (colorFrame != null)
                    {
                        FrameDescription colorFrameDescription = colorFrame.FrameDescription;

                        using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
                        {

                            this.colorBitmap.Lock();

                            // verify data and write the new color frame data to the display bitmap
                            if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight))
                            {
                                colorFrame.CopyConvertedFrameDataToIntPtr(
                                    this.colorBitmap.BackBuffer,
                                    (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4),
                                    ColorImageFormat.Bgra);

                                this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight));
                            }
                        }
                        this.colorBitmap.Unlock();

                    }

            }


    }
    private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e)
    {
    }
    public System.Windows.Media.Imaging.WriteableBitmap image1
    {
        get{
            return this.colorBitmap;
            }
    }
    public void ImageSource()
    {
        WriteableBitmap astreisco = image1;
        Image<Bgr, Byte> image;
        Bitmap pene;
        MemoryStream outStream = new MemoryStream();
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create((BitmapSource)astreisco));
        enc.Save(outStream);
        pene = new Bitmap(outStream);
        image = new Image<Bgr, Byte>(pene);
        this.frame = image.Mat;
        verify = 1;
    }
}
内部类图像处理
{
专用KinectSensor KinectSensor=null;
私有ColorFrameReader ColorFrameReader=null;
private WriteableBitmap colorBitmap=null;
私有Mat帧=空;
私有int验证=0;
公共Mat对象REC(Mat对象)
{
龙我;
ObjectRecog();
《睡眠》(2000年);
ImageSource();
返回ImageProcessingExt.RecogObject.Draw(Object,this.frame,out i);
}
public void ObjectRecog()
{
//获取kinectSensor对象
this.kinectSensor=kinectSensor.GetDefault();
//打开彩色边框的读卡器
this.colorFrameReader=this.kinectSensor.ColorFrameSource.OpenReader();
//帧到达线处理器
this.colorFrameReader.frameArrized+=this.Reader\u colorFrameArrized;
//使用Bgra格式从ColorFrameSource创建colorFrameDescription
FrameDescription colorFrameDescription=this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
//创建要显示的位图
this.colorBitmap=新的WriteableBitmap(colorFrameDescription.Width,colorFrameDescription.Height,96.0,96.0,PixelFormats.Bgr32,null);
//设置IsAvailableChanged事件通知程序
this.kinectSensor.IsAvailableChanged+=this.Sensor\u IsAvailableChanged;
//打开传感器
这个.kinectSensor.Open();
}
私有无效读取器\u ColorFrameArrivedEventArgs e(对象发送方,ColorFrameArrivedEventArgs e)
{
//彩色边框是可识别的
使用(ColorFrame ColorFrame=e.FrameReference.AcquireFrame())
{
if(colorFrame!=null)
{
FrameDescription colorFrameDescription=colorFrame.FrameDescription;
使用(KinectBuffer colorBuffer=colorFrame.LockRawImageBuffer())
{
这个.colorBitmap.Lock();
//验证数据并将新的色框数据写入显示位图
if((colorFrameDescription.Width==此.colorBitmap.PixelWidth)&&(colorFrameDescription.Height==此.colorBitmap.PixelHeight))
{
colorFrame.CopyConvertedFrameDataToIntPtr(
this.colorBitmap.BackBuffer,
(uint)(colorFrameDescription.Width*colorFrameDescription.Height*4),
ColorImageFormat.Bgra);
this.colorBitmap.AddDirtyRect(新的Int32Rect(0,0,this.colorBitmap.PixelWidth,this.colorBitmap.PixelHeight));
}
}
这个.colorBitmap.Unlock();
}
}
}
专用空隙传感器\u IsAvailableChanged(对象发送器,IsAvailableChangedEventArgs e)
{
}
public System.Windows.Media.Imaging.WriteableBitmap image1
{
得到{
返回此.colorBitmap;
}
}
public void ImageSource()
{
WriteableBitmap Astereisco=image1;
图像;
青霉烯;
MemoryStream outStream=新的MemoryStream();
BitMapEnc=新的BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create((BitmapSource)astreisco));
加密保存(扩展);
pene=新位图(扩展);
图像=新图像(pene);
this.frame=image.Mat;
验证=1;
}
}
}

我在this.colorBitmap.Lock()处得到错误,它表示另一个子进程正在使用该colorBitap。我试过很多东西,但没人奏效


Kinect代码是从MSDN复制的。

您使用的是线程吗????还有,
Main
函数在哪里???