2014-02-20 128 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屬性。