2012-03-07 94 views
2

我試圖在WPF應用程序中顯示來自Kinect的攝像頭源。但是,圖像顯示爲空白。將圖像源綁定到BitmapSource時,圖像保持空白

下面是我在我的Kinect類中的一個片段,它全部正確啓動,並且BitmapSource似乎創建正常。

public delegate void FrameChangedDelegate(BitmapSource frame); 
public event FrameChangedDelegate FrameChanged; 


//this event is fired by the kinect service, and fires correctly 

void sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e) 
    { 
     using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 
     { 
      if (colorFrame == null) 
      { 
       return; 
      } 

      byte[] pixels = new byte[colorFrame.PixelDataLength]; 

      colorFrame.CopyPixelDataTo(pixels); 

      int stride = colorFrame.Width * 4; 

      BitmapSource newBitmap = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 
       96, 96, PixelFormats.Bgr32, null, pixels, stride); 
      counter++; 

      //below is the call to the delegate 

      if (FrameChanged != null) 
      { 
       FrameChanged(newBitmap); 
      } 


     } 
    } 

這是我在我的ViewModel。

void kinectService_FrameChanged(BitmapSource frame) 
    { 

     image = frame; 
    } 

    BitmapSource image; 
    public BitmapSource Image 
    { 
     get { return this.image; } 
     set 
     { 
      this.image = value; 
      this.OnPropertyChanged("Image"); 
     } 
    } 

以下是我的XAML視圖。

<Image Canvas.Left="212" Canvas.Top="58" Height="150" Name="image1" Stretch="Fill"  Width="200" Source="{Binding Path=Image}"/> 

所有的事件和屬性似乎都被更新。我究竟做錯了什麼?

+0

嘗試FrameChanged:this.Image = frame – 2012-03-07 17:29:18

回答

2
image = frame; 

應該是:

Image = frame; 

否則你的屬性更改通知,將不會觸發。

+0

哦,親愛的。我無法相信我從未發現過這一點。非常感謝! – benjgorman 2012-03-07 17:31:20

1

嘗試改變:

void kinectService_FrameChanged(BitmapSource frame) 
{ 
    this.image = frame; 
} 

void kinectService_FrameChanged(BitmapSource frame) 
{ 
    this.Image = frame; 
} 

因爲你不使用你的財產,該PropertyChanged事件將永遠不會被調用,所以UI不會知道它需要獲得新的圖像價值。

1

我不知道,但並不需要使用大寫字母I:

void kinectService_FrameChanged(BitmapSource frame) 
{ 
    Image = frame; 
} 

忘了補充:這就是爲什麼WPF stucks。所有這些小陷阱和陷阱。我很少在應用程序中出現「同步」問題,因爲綁定本應阻止,現在我卻遇到了綁定本身的一些小問題。

+0

完全同意,主代碼全部正常工作,並且您遇到了一個您不會注意的小問題。主要是因爲你認爲它必須是一個複雜的問題。 – benjgorman 2012-03-07 17:37:35