C# 如何将图像文件绘制/覆盖到位图图像?

C# 如何将图像文件绘制/覆盖到位图图像?,c#,wpf,image,bitmap,overlay,C#,Wpf,Image,Bitmap,Overlay,我有一个来自Kinect传感器的视频馈送,由一个存储为位图的图像托管。我的问题是如何将图像(例如.png)覆盖到视频提要上 视频提要如下图所示,作为位图源,我知道如何画一条线到位图,但如何从资源到位图绘制图像 KinectVideo.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, colorData, co

我有一个来自Kinect传感器的视频馈送,由一个存储为位图的图像托管。我的问题是如何将图像(例如
.png
)覆盖到视频提要上

视频提要如下图所示,作为位图源,我知道如何画一条线到位图,但如何从资源到位图绘制图像

KinectVideo.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                    PixelFormats.Bgr32, null, colorData, colorFrame.Width * colorFrame.BytesPerPixel); 
下面是我试图通过将图像放置在视频提要上实现的一个模型:

更新了绘图方法的实现,我认为这不是正确的实现,而且在向
添加图像路径时,我遇到无效参数错误。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; 


            }
        }

要创建合并图像,您可以使用提供的方法,如
DrawText
DrawImage
,然后使用
RenderTargetBitmap进行渲染。渲染

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控件上显示
图像
,则可以在其他UI元素之后声明一个图像,或设置
面板。ZIndex
属性:

<Grid>
    <Border Background="Black" />
    <Image Source="/AppName;component/Images/ImageName.jpg" Width="50" Height="50" />
</Grid>

或:



要了解如何将
位图图像
数据绑定到
图像。ItemsSource
属性,请参阅此处有关StackOverflow的问题。要将
图像定位到特定位置,可以使用
Image.Margin
属性或将其放置在
画布中,然后使用
Canvas.Left
Canvas.Top
属性。

好的,因此我将此代码放在我的视频源之后,并设置
drawingContext.drawingImage(KinectVideo,destRect1)
drawingContext.DrawImage(PunchBag,destruct1)这会将图像绘制到提要上,还是我设置错了?好吧,我尝试了更新的代码,但这行代码让我感到困惑,`drawingContext.DrawImage(“Images/bbag.jpg”,destruct2);`什么是destRect2,我是否必须声明它也是图像的路径图像2的正确参数?绘制bitmapSource的区域。这意味着您可以在目标中想要的位置绘制它,并且可能仅从上/左开始绘制它的一部分。将X/Y设置为您想要的位置,宽度/高度以匹配覆盖图像的尺寸。根据我的回答,@dkozl和其他回答者,我认为您有很多解决问题的方法。如果您仍然无法正确回答,请更新您的问题,但这次使用代码块,而不仅仅是一行代码。这就是我要编写的内容,您无法将路径放入图像,您需要从中创建
BitmapImage
<Grid>
    <Image Source="/AppName;component/Images/ImageName.jpg" 
        Width="50" Height="50" Panel.ZIndex="1" />
    <Border Background="Black" />
</Grid>