如何使用纹理图像.jpg作为Windows.Media.pen C#

时间:2014-02-28 03:07:36

标签: c# image jpeg kinect drawingcontext

我的纹理图像为.jpg,我想将此图像用作Windows.Media.pen

我使用Windows.Media.pen在DrawingContext中绘制骨架数据,我从microsoft kinect获取它。

如何将纹理图像.jpg用作Windows.Media.pen?


解决了这个问题。

ImageSource image = new BitmapImage(new Uri(@"...\texture.jpg", UriKind.Relative));
var brush = new ImageBrush(image);
var pen = new Pen(brush, 10);
drawingContext.DrawLine(pen, XPos, YPos);

1 个答案:

答案 0 :(得分:2)

欢迎使用StackOverflow:D

不确定您是否得到了您所期望的内容,如下所示,但这是如何做到的:

您需要使用ImageBrush才能将图片分配给Pen

原始图片:

enter image description here

结果:

enter image description here

代码:

ImageSource image = new BitmapImage(new Uri(@"..\..\5c5f910416e2b92bb73fa59c56fe695d.png", UriKind.Relative));
var brush = new ImageBrush(image);
var pen = new Pen(brush, 50);
var drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
    drawingContext.DrawRectangle(null, pen, new Rect(new Size(200, 200)));
}

var renderTargetBitmap = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
Content = new Image {Source = renderTargetBitmap, Stretch = Stretch.None};