none
Buffer Size is Not Sufficient With WriteableBitmap (MSDN Sample) RRS feed

  • Question

  • I have been starting to learn Kinect programming, and I have been following the article here: http://msdn.microsoft.com/en-us/library/jj131027.aspx

    Unfortunately, when I run the code, I get the following error:

    "An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll

    Additional information: Buffer size is not sufficient."

    What is up with this? As far as I can tell, I followed the example perfectly. Why doesn't this work?

    Here is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Kinect;
    using Microsoft.Kinect.Interop;
    
    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        /// 
        
        public partial class MainWindow : Window
        {
            
    
            public MainWindow()
            {
                InitializeComponent();
                
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                KinectBase hbiuyghuiy = new KinectBase();
                hbiuyghuiy.EnumKinect();
            }
        }
    
        public class KinectBase
        {
           
            KinectSensor sensor;
            
            public void EnumKinect()
            {
                foreach (var potentialSensor in KinectSensor.KinectSensors)
                    {
                        if (potentialSensor.Status == KinectStatus.Connected)
                            {
                                this.sensor = potentialSensor;
                                break;
                            }
                    }
                if (this.sensor != null)
                {
                    this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                    this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    this.sensor.SkeletonStream.Enable();
                    this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
                 
                }
                if (this.sensor != null)
                {
                    this.sensor.Start();
                    this.sensor.ColorFrameReady += SensorColorFrameReady;
                }
                
                    
                
    
    
            }
            private WriteableBitmap colorBitmap;
            
            private byte[] colorPixels;
            private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
                {
                    
                    colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];
                    using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
                        {
                            if (colorFrame != null)
                            {
                                colorFrame.CopyPixelDataTo(colorPixels);
                                this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
    
    
                            }
                            if (colorFrame != null)
                            {
                                // Write the pixel data into our bitmap
                                this.colorBitmap.WritePixels(
                                  new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                                  this.colorPixels,
                                  this.colorBitmap.PixelWidth*sizeof(int), 0);
                                
                                
                            }
                        
                        }
                
                
    
                }
            
        }
    }
       
    

    Monday, February 25, 2013 9:19 PM

Answers

  • How to avoid the System.ArgumentException "Buffer site is not sufficient"

    http://msdn.microsoft.com/en-us/library/jj663792.aspx

    That isn't quite the issue I was having, as I was using the RGB format.

    Fortunately, I had a brain blast while reading through that, and found what I can only assume is an error in the example code. I had (and the example had):

      if (this.sensor != null)
                {
                    this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                    this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    this.sensor.SkeletonStream.Enable();
                    this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
                 
                }
    

    When I should have had:

      if (this.sensor != null)
                {
                    this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                    this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    this.sensor.SkeletonStream.Enable();             
                }
    

    • Marked as answer by NathanCastle Monday, February 25, 2013 10:38 PM
    Monday, February 25, 2013 10:38 PM

All replies

  • How to avoid the System.ArgumentException "Buffer site is not sufficient"

    http://msdn.microsoft.com/en-us/library/jj663792.aspx

    Monday, February 25, 2013 10:13 PM
  • How to avoid the System.ArgumentException "Buffer site is not sufficient"

    http://msdn.microsoft.com/en-us/library/jj663792.aspx

    That isn't quite the issue I was having, as I was using the RGB format.

    Fortunately, I had a brain blast while reading through that, and found what I can only assume is an error in the example code. I had (and the example had):

      if (this.sensor != null)
                {
                    this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                    this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    this.sensor.SkeletonStream.Enable();
                    this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30);
                 
                }
    

    When I should have had:

      if (this.sensor != null)
                {
                    this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                    this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                    this.sensor.SkeletonStream.Enable();             
                }
    

    • Marked as answer by NathanCastle Monday, February 25, 2013 10:38 PM
    Monday, February 25, 2013 10:38 PM