如何绘制/覆盖图像文件位图图像? [英] How to draw/overlay an image file to a bitmap image?

查看:174
本文介绍了如何绘制/覆盖图像文件位图图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由存储为位图图像主办的Kinect感应器的视频饲料。我的问题是我怎么叠加的图像,例如巴纽到视频源。

视频输入显示像下面显示位图源,我知道如何画一条线位图,但如何从资源绘制图像的吧?

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

下面是一个模拟起来什么,我试图通过将图像通过视频输入来实现的:

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

 无效myKinect_ColorFrameReady(对象发件人,ColorImageFrameReadyEventArgs E)
        {
            使用(ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {                如果(colorFrame == NULL)回报;
                字节[] = colorData新的字节[colorFrame.PixelDataLength]
                colorFrame.CopyPixelDataTo(colorData);                 KinectVideo.Source = BitmapSource.Create(colorFrame.Width,colorFrame.Height,96,96,
                    PixelFormats.Bgr32,空,colorData,colorFrame.Width * colorFrame.BytesPerPixel);                矩形destRect2;               //绘制图像叠加到视频馈送
                 VAR drawingVisual =新DrawingVisual();
                 变种的DrawingContext = drawingVisual.RenderOpen();
                 drawingContext.DrawImage(BitmapSource.Create(colorFrame.Width,colorFrame.Height,96,96,PixelFormats.Bgr32,空,colorData,colorFrame.Width * colorFrame.BytesPerPixel)
                                           新的矩形(新尺寸(colorFrame.Width,colorFrame.Height)));
                 drawingContext.DrawImage(图像/ boxbag.jpg,destRect2);
                 drawingContext.Close();
                 VAR mergedImage =新RenderTargetBitmap(colorFrame.Width,colorFrame.Height,96,96,PixelFormats.Pbgra32);
                 mergedImage.Render(drawingVisual);                 KinectVideo.Source = mergedImage;
            }
        }


解决方案

要创建合并后的图像可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext%28v=vs.110%29.aspx\"相对=nofollow> 的DrawingContext ,让你喜欢的方法 DrawText的的DrawImage ,然后使用使其 RenderTargetBitmap.Render

  VAR drawingVisual =新DrawingVisual();
变种的DrawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(BitmapSource.Create(colorFrame.Width,colorFrame.Height,96,96,PixelFormats.Bgr32,空,colorData,colorFrame.Width * colorFrame.BytesPerPixel)
                          新的矩形(新尺寸(colorFrame.Width,colorFrame.Height)));
VAR窗格在overlayImage =新的BitmapImage(新的URI(图像/ boxbag.jpg));
drawingContext.DrawImage(窗格在overlayImage,
                          新的矩形(X,Y,overlayImage.Width,overlayImage.Height));
drawingContext.Close();
VAR mergedImage =新RenderTargetBitmap(colorFrame.Width,colorFrame.Height,96,96,PixelFormats.Pbgra32);
mergedImage.Render(drawingVisual);KinectVideo.Source = mergedImage;

I have a video feed from a Kinect sensor hosted by an image stored as a bitmap. My question is how do I overlay an image, for example a .png on to the video feed.

The video feed is shown like show below as bitmap source, I know how to draw a line to the bitmap but how do I draw an image from resources to the it?

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

Below is a mock up of what I'm trying to achieve by placing the image over the video feed:

Updated implementation of drawing method,I don't think this is the correct implementation also I'm getting invalid argument error when adding image path to .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; 


            }
        }

解决方案

To create merged image you can use DrawingContext that gives you methods like DrawText or DrawImage and then render it using 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;

这篇关于如何绘制/覆盖图像文件位图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆