I am trying to use EMGUCV for C#. Currently i have installed Visual Studio 2010 express Edition. When trying to execute some simple commands the Emgu.CV.CvInvoke
threw an exception came out so i put the unmanaged code in the exe folder. But still it continued to give me the error. So i tried adding the unmanaged code to solution explorer and still it is giving me this error. Is there anything else which i can do so i can finally use emguCV?
The exception is
System.TypeInitializationException was unhandled
Message=The type initializer for 'Emgu.CV.CvInvoke' threw an exception.
having stack trace:
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
I am only executing some simple code being:
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();
}
}
The examples of emguCV work on my computer. Thanks Alot Adrian
The following error: "An attempt was made to load a program with an incorrect format." is shown if the unmanaged assembly is compiled for different platform than your .NET code is currently executing on.
For example: take some dll that is compiled for x86, import some function call to C# and run. If you have 64-bit Windows this will fail, because .NET code is compiled to x64.
To overcome this you have two possibilities: 1) to use unmanaged dll version that matches your platform 2) Project -> Properties -> Build -> tick the platform that matches the platform for which an unmanaged dll is compiled (x86, x64) (it is probably set to Any CPU).
Copy your unmanaged dlls into \bin\Debug and/or \bin\Release project, not into some subfolder!! If you really want to use dlls from some subfolder, than edit the process environment variable like this:
Environment.SetEnvironmentVariable(path, Environment.GetEnvironmentVariable(path) + Path.PathSeparator + );
this will add your subfolder into search path. This variable is process variable meaning that all changes will be undone when process stops executing.