如何将图像文件绘制/叠加到位图图像?

我有一个Kinect传感器的video输入,该传感器由存储为位图的图像托管。 我的问题是如何将图像叠加到video源上,例如.png

video输入显示如下图所示为位图源,我知道如何在位图上绘制一条线但是如何从资源中绘制图像呢?

 KinectVideo.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel); 

下面是通过将图像放在video源上来模拟我想要实现的目标:

拳击包是我想要叠加到视频源的图像。

更新了绘图方法的实现,我不认为这是正确的实现我在将图像路径添加到.DrawImage时得到无效的参数错误:

 void myKinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame == null) return; byte[] colorData = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo(colorData); KinectVideo.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel); Rect destRect2; //drawing image overlay to video feed var drawingVisual = new DrawingVisual(); var drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawImage(BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel), new Rect(new Size(colorFrame.Width, colorFrame.Height))); drawingContext.DrawImage("Images/boxbag.jpg", destRect2); drawingContext.Close(); var mergedImage = new RenderTargetBitmap(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Pbgra32); mergedImage.Render(drawingVisual); KinectVideo.Source = mergedImage; } } 

要创建合并图像,您可以使用DrawingContext为您提供DrawTextDrawImage等方法,然后使用RenderTargetBitmap.Render渲染它:

 var drawingVisual = new DrawingVisual(); var drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawImage(BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel), new Rect(new Size(colorFrame.Width, colorFrame.Height))); var overlayImage = new BitmapImage(new Uri("Images/boxbag.jpg")); drawingContext.DrawImage(overlayImage, new Rect(x, y, overlayImage.Width, overlayImage.Height)); drawingContext.Close(); var mergedImage = new RenderTargetBitmap(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Pbgra32); mergedImage.Render(drawingVisual); KinectVideo.Source = mergedImage; 

如果您只想在另一个UI控件上显示一个Image ,那么您可以在其他UI元素之后声明一个,或者设置Panel.ZIndex属性:

     

要么:

     

要了解如何将BitmapImage数据绑定到Image.ItemsSource属性,请在StackOverflow上查看Bind Xaml位图图像问题。 要将Image定位在特定位置,您可以使用Image.Margin属性或将其放在Canvas并使用Canvas.LeftCanvas.Top属性。