c# - 将图像源绑定(bind)到 BitmapSource 时,图像保持空白

标签 c# wpf bitmapsource

我正在尝试在 WPF 应用程序中显示来自 Kinect 的摄像头画面。但是,图像显示为空白。

下面是我在 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 View 中的内容。

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

所有事件和属性似乎都已更新。我做错了什么?

最佳答案

image = frame;

应该是:

Image = frame;

否则您的属性更改通知将不会触发。

关于c# - 将图像源绑定(bind)到 BitmapSource 时,图像保持空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606290/

相关文章:

wpf - 无论新值如何,当属性更改时触发数据触发器

c# - 将图像附加到剪贴板

c# - 使用 VisualBrush 获取 WPF 区域的 System.Drawing.Bitmap

c# - C#如何解析重载的构造函数: Foo(Exception) and Foo(object)?

c# - 使用 C# 启动和停止服务

c# - 是否可以在 C# 中创建扩展运算符?

c# - 我怎样才能在一个类中拥有一个属性来存储图片/图像?

c# - 动态创建图像

c# - 列表框内的统一网格

wpf - 您如何确保 WPF 从内存中释放大型 BitmapSource?