2012-09-19 105 views
2

我正在嘗試爲C#使用EMGUCV。目前我已經安裝了Visual Studio 2010 Express Edition。當試圖執行一些簡單的命令時,Emgu.CV.CvInvoke拋出一個異常,所以我把非託管代碼放在exe文件夾中。但它仍然繼續給我錯誤。所以我嘗試將非託管代碼添加到解決方案資源管理器,但它仍然給我這個錯誤。還有什麼我可以做的,所以我終於可以使用emguCV?Emgu.CV.CvInvoke'拋出異常

唯一的例外是

System.TypeInitializationException was unhandled 
    Message=The type initializer for 'Emgu.CV.CvInvoke' threw an exception. 

具有堆棧跟蹤:

at Emgu.CV.CvInvoke.cvCreateCameraCapture(Int32 index) 
    at Emgu.CV.Capture..ctor(Int32 camIndex) in C:\Program Files (x86)\Emgu\libemgucv-windows-x64-2.2.1.1150\Emgu.CV\Capture\Capture.cs:line 105 
    at Emgu.CV.Capture..ctor() in C:\Program Files (x86)\Emgu\libemgucv-windows-x64-2.2.1.1150\Emgu.CV\Capture\Capture.cs:line 93 
    at cameraWorks.Form1.camButton_Click(Object sender, EventArgs e) in C:\Users\Adrian\documents\visual studio 2010\Projects\cameraWorks\cameraWorks\Form1.cs:line 38 
    at System.Windows.Forms.Control.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnClick(EventArgs e) 
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
    at System.Windows.Forms.Button.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(Form mainForm) 
    at cameraWorks.Program.Main() in C:\Users\Adrian\documents\visual studio 2010\Projects\cameraWorks\cameraWorks\Program.cs:line 18 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

的InnerException:

InnerException: System.BadImageFormatException Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) Source=Emgu.CV StackTrace: at Emgu.CV.CvInvoke.cvRedirectError(CvErrorCallback errorHandler, IntPtr userdata, IntPtr prevUserdata) at Emgu.CV.CvInvoke..cctor() in C:\Program Files (x86)\Emgu\libemgucv-windows-x64-2.2.1.1150\Emgu.CV\PInvoke\CvInvoke.cs:line 50

我只是執行一些簡單的代碼之中:

public partial class Form1 : Form 
{ 
    private Capture capture; 
    private bool captureInProgress; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void ProcessFrame(Object sender, EventArgs args) 
    { 
     Image<Bgr, Byte> ImageFrame = capture.QueryFrame(); 
     CamImageBox1.Image = ImageFrame; 
    } 

    private void camButton_Click(object sender, EventArgs e) 
    { 
     if (capture == null) 
     { 
      try 
      { 
       capture = new Capture(); 
      } 
      catch (NullReferenceException excpt) 
      { 
       MessageBox.Show(excpt.Message); 
      } 
     } 

     if (capture != null) 
     { 
      if (captureInProgress) 
      { 
       camButton.Text = "start"; 
      } 
      else 
      { 
       camButton.Text = "stop"; 
       Application.Idle += ProcessFrame; 
      } 
      captureInProgress = !captureInProgress; 
     } 
    } 

    private void ReleaseData() 
    { 
     if (capture != null) 
     { 
      capture.Dispose(); 
     } 
    } 

emguCV的例子在我的電腦上工作。 非常感謝 Adrian

+1

這個例外的細節可能有幫助嗎? – John3136

+0

究竟是什麼異常?消息和堆棧跟蹤將會有所幫助。 –

+0

done編輯stacktrace和異常 –

回答

2

以下錯誤:「試圖加載程序的格式不正確。」如果非託管程序集針對不同的平臺進行編譯,而不是您的.NET代碼當前正在執行,則會顯示該列表。

例如: 取一些爲x86編譯的dll,將一些函數調用導入C#並運行。如果你有64位Windows,這將失敗,因爲.NET代碼被編譯爲x64。

爲了克服這一點,你有兩種可能性: 1)使用該平臺 2相匹配的非託管DLL版本)項目 - >屬性 - >建設 - >打勾,對於其中的非託管的DLL編譯平臺相匹配的平臺(x86,x64)(它可能設置爲任何CPU)。


將您的非託管dll複製到\​​ bin \ Debug和/或\ bin \ Release項目中,而不是放到某個子文件夾中! 如果你真的想從某個子文件夾使用dll,比編輯像這樣的進程環境變量:

Environment.SetEnvironmentVariable(path,Environment.GetEnvironmentVariable(path)+ Path.PathSeparator +);

這會將您的子文件夾添加到搜索路徑中。該變量是流程變量,意味着當流程停止執行時,所有更改都將被撤消。