I'm trying to show a camera feed from a Kinect in a WPF application. However, the image appears blank.

Below is a snippet of what I have in my Kinect class, this all fires correctly, and the BitmapSource appears to be created fine.

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);
            }


        }
    }

Here is what I have in my ViewModel.

    void kinectService_FrameChanged(BitmapSource frame)
    {

        image = frame;
    }

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

And below is what I have in my XAML View.

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

All of the events and property's seem to be updated. What am I doing wrong?

有帮助吗?

解决方案

image = frame;

should be:

Image = frame;

otherwise your property change notification won't fire.

其他提示

Try changing:

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

to

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

Because you aren't using your property, the PropertyChanged event will never be invoked, so the UI won't know that it needs to get the new image value.

I am not sure but don't you need to use a capital I:

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

forgot to add: this is why WPF stucks. All these little traps and gotchas. I rarely had "sync" problems in applications that binding is supposed to prevent and now instead I get loads of little problems with the binding itself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top