1

I need to replace the background pixels which are not players with an Image, I can successfully replace the user pixel with the ColorFrameImage, but as soon as i try to replace the rest of the depth bits with the image pixels . . I get a XamlParseError exception

Update: The ims.PixelHeight & ims.PixelWidth are causing the exception . .

I maybe doing the Bitmap image to pixel conversion wrong. .

Any work around?

Here is the code:

public partial class MainWindow : Window
{

    Byte[] pixels;

    Byte[] pix;

    Byte[] pixy;

    BitmapImage ims;

    public MainWindow()
    {
        InitializeComponent();


        ims = new BitmapImage();
        ims.BeginInit();
        ims.UriSource = new Uri(@"/autumn_scene.jpg", UriKind.RelativeOrAbsolute);

        //int height = ims.PixelHeight;
       // int width = ims.PixelWidth;
        //int nStride = (ims.PixelWidth * ims.Format.BitsPerPixel + 7) / 8;

      //  pixy = new byte[(ims.PixelHeight* 4+1)];

      //  MessageBox.Show("Pixel Height" + ims.PixelHeight);
        MessageBox.Show("I hate Exceptions");

        //System.Console.WriteLine("Pixel Height" + ims.PixelHeight);

   //     ims.CopyPixels(pixy, nStride, 0);

      //  ims.EndInit();

        }




    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        kinectSensorChooser1.KinectSensorChanged += new DependencyPropertyChangedEventHandler(kinectSensorChooser1_KinectSensorChanged);
    }

    void kinectSensorChooser1_KinectSensorChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        KinectSensor oldSensor = (KinectSensor)e.OldValue;
        StopKinect(oldSensor);

        KinectSensor newSensor = (KinectSensor)e.NewValue;

        //NOTE: Add this check for a null sensor
        if (newSensor == null)
        {
            return;
        }


        newSensor.ColorStream.Enable();
        newSensor.DepthStream.Enable();
        newSensor.SkeletonStream.Enable();
        newSensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(newSensor_AllFramesReady);

        try 
{
    newSensor.Start();
}
catch (System.IO.IOException)
{

 kinectSensorChooser1.AppConflictOccurred();
}
    }

    void newSensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
    {

    using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (depthFrame == null)
            {
                return;
            }

            if (colorFrame == null)
            {
                return;
            }


            pixels = GenerateColoredBytes(depthFrame,colorFrame);

            //pix = new byte[colorFrame.PixelDataLength];
            //colorFrame.CopyPixelDataTo(pix);

            int stride2 = colorFrame.Width * 4;

            image1.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride2);

            //Number of bytes per row
            int stride = depthFrame.Width * 4;


           image2.Source =  BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
          //  image2.Source = ims;
        }


    }

   private byte[] GenerateColoredBytes(DepthImageFrame depthFrame, ColorImageFrame colorFrame)

    {



        //Getting raw depth data

        short[] rawDepthData = new short[depthFrame.PixelDataLength];
        depthFrame.CopyPixelDataTo(rawDepthData);



        pixels = new byte[depthFrame.Width * depthFrame.Height * 4];

        pix = new byte[colorFrame.Width * colorFrame.Height * 4];




        colorFrame.CopyPixelDataTo(pix);

     //  ColorImagePoint mapval = depthFrame.MapToColorImagePoint(depthFrame.Width,depthFrame.Height,
        //Getting & Setting the BGR values
        const int BlueIndex = 0;
        const int GreenIndex = 1;
        const int RedIndex = 2;
     //   const int alpha = 3;



       // int nStride = (ims.PixelWidth * ims.Format.BitsPerPixel + 7) / 8;

       //byte[] pixy = new byte[ims.PixelHeight * nStride];

       // ims.CopyPixels(pixy,nStride,0);


       for (int depthIndex = 0, colorIndex = 0; depthIndex < rawDepthData.Length && colorIndex < pixels.Length; depthIndex++, colorIndex += 4) {
            //Player formula
            int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;

            //Depth / Distance formula
            int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;


            if (player > 0)
            {
                pixels[colorIndex + BlueIndex] = pix[colorIndex + BlueIndex];
                pixels[colorIndex + GreenIndex] = pix[colorIndex + GreenIndex];
                pixels[colorIndex + RedIndex] = pix[colorIndex + RedIndex];

              //  pixels[colorIndex + BlueIndex] = 
            }else{

                //if (colorIndex + RedIndex < pixy.Length)
                //{

                //    pixels[colorIndex + BlueIndex] = pixy[colorIndex + BlueIndex];
                //    pixels[colorIndex + GreenIndex] = pixy[colorIndex + GreenIndex];
                //    pixels[colorIndex + RedIndex] = pixy[colorIndex + RedIndex];
                //}
                //else
                {
                    pixels[colorIndex + BlueIndex] = 255;
                    pixels[colorIndex + GreenIndex] = 0;
                    pixels[colorIndex + RedIndex] = 0;
                    //   pixels[colorIndex + alpha] = 0;
                }

            }
       }

            return pixels;
   }

void StopKinect(KinectSensor sensor){
        if (sensor != null)
        {
            //NOTE: Add this code
            if (sensor.IsRunning)
            {
                sensor.Stop();
                if (sensor.AudioSource != null)
                {
                    sensor.AudioSource.Stop();
                }
            }
        }
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        StopKinect(kinectSensorChooser1.Kinect);
    }
}
VMAtm
  • 27,943
  • 17
  • 79
  • 125
Sweta Dwivedi
  • 326
  • 2
  • 4
  • 20
  • 1
    Since it's a XamlParseException (http://msdn.microsoft.com/en-US/library/system.xaml.xamlparseexception.aspx), you should provide the XAML file additionally the code behind. – Chris Ortner Jul 18 '12 at 11:01
  • Don't you have to call ims.EndInit(); before you try to retrieve the width and the height of the BitmapImage ? (see: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.urisource.aspx) – Renaud Dumont Sep 25 '12 at 12:17

0 Answers0