2012-03-07 15 views
2

私は、KinectからのカメラフィードをWPFアプリケーションで表示しようとしています。ただし、画像は空白になります。イメージソースを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

フレームで試してみてください:this.Image = frame –

答えて

2
image = frame; 

は次のようになります。

Image = frame; 

そうでない場合は、あなたのプロパティ変更通知が発生しません。

+0

ああああ。私はこれを見つけたことがないとは信じられません。巨大な感謝! – benjgorman

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です。これらのすべての小さなトラップとgotchas。私はめったにバインディングが防止するはずのアプリケーションでは "同期"の問題を抱えていましたが、代わりにバインディング自体にほとんど問題がありません。

+0

完全に一致していれば、メインコードはすべて動作し、気付かない小さな問題が残っています。主にあなたが複雑な問題でなければならないと思っているからです。 – benjgorman

関連する問題