2014-02-20 130 views
2

我有一个由位图存储的图像托管的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.

更新执行绘制方法的,我不认为这

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; 


      } 
     } 

回答

4

要创建:在正确的实施也增加图像路径.DrawImage当我收到无效参数错误合并后的图像可以渲染使用RenderTargetBitmap.Render它使用DrawingContext,让你像DrawTextDrawImage然后方法:

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; 
+0

好吧,所以我把这段代码放在我的视频源后面并设置'drawingContext.DrawImage(KinectVideo,destRect1);''和'drawingContext.DrawImage(PunchBag,destRect1);',这会将图像绘制到Feed上或忘了这个错误? –

+1

请检查我的更新 – dkozl

+0

好吧,我试过更新后的代码,但是这一行令我困惑,'drawingContext.DrawImage(“Images/bbag.jpg”,destRect2);'什么是destRect2,我必须声明它是图像路径图像2的正确参数? –

1

如果你只是想在另一个UI控件的顶部显示Image,那么你可以只申报一个后,其他用户界面元素,或设置Panel.ZIndex属性:

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

或者:

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

要了解如何将BitmapImage绑定到Image.ItemsSource属性,请参阅StackOverflow上的Bind Xaml bitmap image问题。要将Image放置在特定位置,您可以使用Image.Margin属性,也可以将其放入Canvas中,并使用Canvas.LeftCanvas.Top属性。