2012-07-24 74 views
0

我新的WPF> 我有項目,通過使用OpenCV庫拋網民> 解碼QR碼,現在成功運行打開Windows窗體與opcv從WPF窗口

我想使用該項目在新的WPF項目> 添加新的WPF項目,並提及到winFrom應用>

後,這我簡單的代碼來打開winfrom>

public void runnow(){ 
Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false); 
Application.Run(new CameraCapture.cameraCapture()); } 

通過破壞給我這個例外>

爲 'Emgu.CV.CvInvoke' 的類型初始值引發異常。>

我能做些什麼爲解決這個

C#代碼

public partial class CameraCapture : Form 
{ 

Capture capture; 
    bool Capturing; 
    Bitmap bimap; 
    private Reader reader; 
    private Hashtable hint; 
    libAES libEncryption = new libAES(); 
    string Mykey = ""; 
    public static String dataDecrypted=""; 


    public CameraCapture() 
    { 
     InitializeComponent(); 
    } 

    private void Mains(object sender, EventArgs arg) // Start function main to encode Qr code 
    { 
     Image<Bgr, Byte> image = capture.QueryFrame(); 
     if (image != null) 
     { 
      bimap = image.ToBitmap(); 
      pictureBox1.Image = bimap; 
      reader = new QRCodeReader(); 
      hint = new Hashtable(); // Add some elements to the hash table. There are no duplicate keys, but some of the values are duplicates. 
      hint.Add(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); 
      RGBLuminanceSource source = new RGBLuminanceSource(bimap, bimap.Width, bimap.Height); //This class is used to help decode images from files which arrive as RGB data from* Android bitmaps. It does not support cropping or rotation. 
      BinaryBitmap img = new BinaryBitmap(new GlobalHistogramBinarizer(source)); 
      Result result = null; 
      try 
      { 
       result = reader.decode(img, hint); 
       dataDecrypted = libEncryption.Decrypt(result.Text, Mykey); 

      } 
      catch 
      { 
       dataDecrypted = ""; 
      } 
      if (result == null) 
      { 
       label1.Text = " no decode"; 

      } 
      else 
      { 

       label4.Text = result.Text; 
       label1.Text = dataDecrypted; 

       capture.Dispose(); 

      } 
     } 


    } // end function Main 


    private void btnStart_Click(object sender, EventArgs e) 
    { 
     if (capture == null) 
     { 
      try 
      { 
       capture = new Capture(); // **the exption thown here** 
      } 
      catch (NullReferenceException exception) 
      { 
       MessageBox.Show(exception.Message); 
      } 
     } 
     if (capture != null) 
     { 
      if (Capturing) 
      { 
       btnStart.Text = "Start Capture"; 
       Application.Idle -= Mains; 
      } 
      else 
      { 
       btnStart.Text = "Stop Capture"; 
       Application.Idle += Mains; 
      } 
      Capturing = !Capturing; 
     } 
    } 
    private void Release() 
    { 
     if (capture != null) 
      capture.Dispose(); 
    }} 

回答

0

如果您想要在WPF中託管WinForm,則需要使用主機控制System.Windows.Forms.Integration.WindowsFormsHost

WPF提供了許多具有豐富功能集的控件。但是,您可能有時想在您的WPF頁面上使用Windows窗體控件。例如,對於 示例,您可能會在現有的Windows控件上投入大量資金,或者您可能有一個Windows窗體控件,它提供了 的獨特功能。

示例代碼

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
// Create the interop host control. 
System.Windows.Forms.Integration.WindowsFormsHost host = 
    new System.Windows.Forms.Integration.WindowsFormsHost(); 

// Create the MaskedTextBox control. 
MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000"); 

// Assign the MaskedTextBox control as the host control's child. 
host.Child = mtbDate; 

// Add the interop host control to the Grid 
// control's collection of child controls. 
this.grid1.Children.Add(host); 
} 

檢查=> http://msdn.microsoft.com/en-us/library/ms751761.aspx

+0

同樣的事情> **爲 'Emgu.CV.CvInvoke' 的類型初始值引發了異常** – 2012-07-24 15:43:11

+0

能否請您粘貼你試過的一些更多的代碼? – Adil 2012-07-24 15:45:17

+0

我修改了我的問題 – 2012-07-24 15:59:24