2012-07-28 129 views
-1

我正在使用Kinect SDK並嘗試添加顏色框。我正在使用的代碼:爲什麼不顯示彩色圖像?

byte[] pixels = new byte[sensor.ColorStream.FramePixelDataLength]; 

WriteableBitmap image = new WriteableBitmap(sensor.ColorStream.FrameWidth, sensor.ColorStream.FrameHeight, 96, 96, 
     PixelFormats.Bgra32, null); 

video.Source = image; 

colorFrame.CopyPixelDataTo(pixels); 

image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight), pixels, image.PixelWidth * sizeof(int), 0); 

但圖像不顯示。我知道我可以連接到Kinect,因爲我可以改變仰角。我究竟做錯了什麼?提前致謝。 注:我試圖避免使用Coding4Fun

+0

@downvoter有什麼不對? – 2012-08-14 19:37:27

回答

0

不,你是如此親密。我假設video是一個WPF圖像。

private WriteableBitmap wBitmap; 
private byte[] pixels; 

private void WindowLoaded(...) 
{ 
    //set up kinect first, but don't start it 
    ... 

    pixels = new byte[sensor.ColorStream.FramePixelDataLength]; 

    wBitmap = new WriteableBitmap(sensor.ColorStream.FrameWidth, sensor.ColorStream.FrameHeight, 
     96, 96, PixelFormats.Bgra32, null); 

    video.Source = wBitmap; 

    sensor.Start(); 
} 

private void ColorFrameReady(object sender, ColorImageFrameReadyArgs e) 
{ 
    using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 
    { 
     if (colorFrame == null) 
     { 
      return; 
     } 


     colorFrame.CopyPixelDataTo(pixels); 

     wBitmap.WritePixels(new Int32Rect(0, 0, wBitmap.PixelWidth, wBitmap.PixelHeight), 
      pixels, image.PixelWidth * 4, 0); 
    } 
} 

你不應該建立在每一幀新的BitmapSource,它的很多更好的創建之初的單一WriteableBitmap的,只是刷新每一幀。

此外,您以前無法看到圖像的原因實際上並不是因爲它未被刷新。它絕對是,但它是無形的。您將WriteableBitmap格式設置爲Bgra32,其中a是alpha。 Kinect以Bgr32格式發送數據;沒有設置Alpha通道。因此,當您創建Bgra32位圖時,它會看到Kinect將alpha通道設置爲0,因此圖像顯示爲完全透明。