使用Kinect SDK绘制骨架时添加图像而不是椭圆

时间:2016-01-22 18:44:52

标签: c# wpf kinect kinect-sdk

public static void DrawSkeleton(this Canvas canvas, Body body)
{
    foreach (Joint joint in body.Joints.Values)
    {
        canvas.DrawPoint(joint);
    }
}

public static void DrawPoint(this Canvas canvas, Joint joint)
{
    joint = joint.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);

    Ellipse ellipse = new Ellipse
    {
         Width = 20,
         Height = 20,
         Fill = new SolidColorBrush(Colors.LightBlue)
    };

    Canvas.SetLeft(ellipse, joint.Position.X - ellipse.Width / 2);
    Canvas.SetTop(ellipse, joint.Position.Y - ellipse.Height / 2);

    canvas.Children.Add(ellipse);
}

我想从应用程序文件夹路径中添加一个小的jpeg图像,而不是椭圆。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

Image img = new Image
{
    Width = 20,
    Height = 20,
    Source = new BitmapImage(new Uri("your/image/relative/path", UriKind.Relative)),
};

Canvas.SetLeft(img, joint.Position.X - img.Width / 2);
Canvas.SetTop(img, joint.Position.Y - img.Height / 2);

canvas.Children.Add(img);

答案 1 :(得分:1)

您必须将CacheOption设置为OnLoad:

        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.UriSource = new Uri("yourimage.png", UriKind.Relative);
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.EndInit();
        var img = new Image{Source=bitmap};

        canvas.Children.Add(img);