2013-05-16 124 views
0

我試圖按照下面的教程,但使用的不是贏形式WPF:EMGU用C#WPF

A Basic Program

WPF不使用PictureBox,而是使用Image

所以這裏試圖加載一個Image

XAML

<Image x:Name="srcImg" Width="400" Height="300"></Image> 

CS嘗試1:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName); 
srcImg.Source = My_Image.ToBitmap(); 

錯誤消息

Cannot implicitly convert type 'System.Drawing.Bitmap' 
to 'System.Windows.Media.ImageSource' 

CS嘗試2:

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName); 
srcImg.Source = new BitmapImage(My_Image); 

錯誤消息

Error 1 The best overloaded method match for 'System.Windows.Media.Imaging.BitmapImage.BitmapImage(System.Uri)' has some invalid arguments 
Error 2 Argument 1: cannot convert from 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>' to 'System.Uri' 

任何想法我做錯了嗎?

+2

來吧,閱讀你正在使用的東西的文件,也錯誤是清晰的。 –

+0

我不知道EMGU是什麼,但WPF不關心'System.Drawing'的東西,不會使用它。如果您希望在WPF中實現任何功能,請從您的所有項目中刪除對「System.Drawing.dll」的所有引用並重新開始。 –

回答

5

問題解決。

Image<Bgr, Byte> My_Image = new Image<Bgr, byte>(Openfile.FileName); 
srcImg.Source = BitmapSourceConvert.ToBitmapSource(myImage); 

BitmapSourceConvert類:

public static class BitmapSourceConvert 
{ 
    [DllImport("gdi32")] 
    private static extern int DeleteObject(IntPtr o); 

    public static BitmapSource ToBitmapSource(IImage image) 
    { 
     using (System.Drawing.Bitmap source = image.Bitmap) 
     { 
      IntPtr ptr = source.GetHbitmap(); 

      BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       ptr, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 

      DeleteObject(ptr); 
      return bs; 
     } 
    } 
} 
0

你可以得到一個ImageSource from a Bitmap(浪費,但你已經有ToBitmap()),或者你可以實現直接轉換。

+1

你好,我認爲你誤解了這個問題。我試圖顯示存儲在EMGU(OpenCV)圖像中的圖像,而不是從文件中顯示圖像。 – Gravy

+0

當你看起來有路徑時,爲什麼你會嘗試這樣做? –

+0

你知道OpenCV是什麼或EMGU是什麼?我試圖在我開始對其進行更改之前測試我能夠顯示EMGU圖像。 – Gravy

0

如果您想要將Emgu CV與WPF一起使用,您應該考慮使用Emgu的picturebox(此控件僅適用於win窗體)執行用戶控件,然後將其與WindowsFormsHost一起使用。

0

複製BitmapSourceConverter.cs%installfolder%/Emgu/emgucv-windesktop x.x.x/Emgu.CV.WPF,並添加到您的項目轉換成的BitmapSource將圖像轉換。它是完整版本:

//---------------------------------------------------------------------------- 
// Copyright (C) 2004-2017 by EMGU Corporation. All rights reserved. 
//---------------------------------------------------------------------------- 

using System; 
using System.Runtime.InteropServices; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using Emgu.CV; 
using Emgu.CV.CvEnum; 

namespace Emgu.CV.WPF 
{ 
    public static class BitmapSourceConvert 
    { 
     /// <summary> 
     /// Delete a GDI object 
     /// </summary> 
     /// <param name="o">The poniter to the GDI object to be deleted</param> 
     /// <returns></returns> 
     [DllImport("gdi32")] 
     private static extern int DeleteObject(IntPtr o); 

     /// <summary> 
     /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source 
     /// </summary> 
     /// <param name="image">The Emgu CV Image</param> 
     /// <returns>The equivalent BitmapSource</returns> 
     public static BitmapSource ToBitmapSource(IImage image) 
     { 
     using (System.Drawing.Bitmap source = image.Bitmap) 
     { 
      IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap 

      BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       ptr, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 

      DeleteObject(ptr); //release the HBitmap 
      return bs; 
     } 
     } 

     public static Mat ToMat(BitmapSource source) 
     { 

     if (source.Format == PixelFormats.Bgra32) 
     { 
      Mat result = new Mat(); 
      result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 4); 
      source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step*result.Rows, result.Step); 
      return result; 
     } else if (source.Format == PixelFormats.Bgr24) 
     { 
      Mat result = new Mat(); 
      result.Create(source.PixelHeight, source.PixelWidth, DepthType.Cv8U, 3); 
      source.CopyPixels(Int32Rect.Empty, result.DataPointer, result.Step * result.Rows, result.Step); 
      return result; 
     } 
     else 
     { 
      throw new Exception(String.Format("Convertion from BitmapSource of format {0} is not supported.", source.Format)); 
     } 
     } 
    } 
}