gpt4 book ai didi

c# - Emgu.CV.CvInvoke' 抛出异常

转载 作者:行者123 更新时间:2023-11-30 22:25:00 24 4
gpt4 key购买 nike

我正在尝试将 EMGUCV 用于 C#。目前我已经安装了 Visual Studio 2010 速成版。当尝试执行一些简单的命令时,Emgu.CV.CvInvoke 抛出了异常,所以我将非托管代码放在 exe 文件夹中。但它仍然继续给我错误。所以我尝试将非托管代码添加到解决方案资源管理器,但它仍然给我这个错误。还有什么我可以做的,以便我最终可以使用 emguCV 吗?

异常(exception)是

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: 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 的例子在我的电脑上运行。多谢阿德里安

最佳答案

以下错误:“尝试加载格式不正确的程序。”如果非托管程序集是针对与当前执行 .NET 代码的平台不同的平台编译的,则会显示。

例如: 获取一些为 x86 编译的 dll,将一些函数调用导入 C# 并运行。如果您有 64 位 Windows,这将失败,因为 .NET 代码被编译为 x64。

要克服这个问题,您有两种可能性: 1) 使用与您的平台匹配的非托管 dll 版本 2) Project -> Properties -> Build -> 勾选与编译非托管dll的平台(x86,x64)相匹配的平台(它可能设置为Any CPU)。


将您的非托管 dll 复制到\bin\Debug 和/或\bin\Release 项目中,而不是复制到某个子文件夹中!!如果您真的想使用某个子文件夹中的 dll,请像这样编辑进程环境变量:

Environment.SetEnvironmentVariable(路径, Environment.GetEnvironmentVariable(路径) + Path.PathSeparator + );

这会将您的子文件夹添加到搜索路径中。该变量是流程变量,意味着当流程停止执行时,所有更改都将被撤消。

关于c# - Emgu.CV.CvInvoke' 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com