c# - 如何绘制/覆盖图像文件到位图图像?

标签 c# wpf image bitmap overlay

我有一个来自 Kinect 传感器的视频源,该视频由存储为位图的图像托管。我的问题是如何将图像(例如 .png)叠加到视频源上。

视频源显示为位图源,如下所示,我知道如何在位图上画一条线,但如何从资源中绘制图像到位图?

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

下面是我试图通过将图像放在视频源上来实现的模型:

The boxing bag is the image I want to overlay to the video feed.

更新了绘图方法的实现,我认为这不是正确的实现,我在将图像路径添加到 .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;

关于c# - 如何绘制/覆盖图像文件到位图图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21915705/

相关文章:

c# - 异步/等待中的重入?

Java,将传入附件与数据库中的图像列表进行比较,看看是否相同

c# - C# 服务器(不是 Web 服务器)和 PHP 之间的数据交换

C# 如何显示像 : 这样的对象列表

c# - 根据参数,以编程方式启动 CLI 应用程序不起作用

python matplotlib 如何使用类打开图像

ios - 将多个高分辨率图像合并为单个图像时内存增加,iOS

c# - 错误处理的灵活性

WPF 给出不正确的大小值

wpf - 苹果风格的滚动条WPF