使用 EmguCV 导入视频时出现 TypeInitializationException

TypeInitializationException trown while importing Video with EmguCV

我正在开展一个项目,希望用户能够导入视频。每次当我选择要导入的视频时,public class CvString : UnmanagedObject 都会抛出 System.TypeInitializationException 错误。这不取决于数据类型,它不适用于图片,例如 .jpeg.png,也不适用于我尝试过的任何视频格式 (.avi``.mp4``.wmp)

这是我要加载和显示视频的片段(自己编写的代码)

OpenFileDialog ofd = new OpenFileDialog();
Capture _capture;
Timer My_Time = new Timer();
int FPS = 30;
public Form1()
{
    InitializeComponent();
    //Frame Rate
    My_Time.Interval = 1000 / FPS;
    My_Time.Tick += new EventHandler(timer1_Tick);
    My_Time.Start();

    _capture = new Capture("20151102_110553.mp4");

}

private void timer1_Tick(object sender, EventArgs e)
{
    imageBox1.Image = _capture.QueryFrame();
}


private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    _capture = new Capture(openFileDialog1.FileName.ToString());
}

private void button2_Click(object sender, EventArgs e)//Import-Button
{
     openFileDialog1.ShowDialog();
}

这里是抛出异常的方法(来自 Emgu.CV 的代码)

namespace Emgu.CV
{
   public class CvString : UnmanagedObject
   { 
        private bool _needDispose;

        internal CvString(IntPtr ptr, bool needDispose)
        {
             _ptr = ptr;
             _needDispose = needDispose;
        }

        public CvString(String s)
        {
        if (s == null)
            {
                _ptr = CvInvoke.cveStringCreate();
            }
        else
        {
            byte[] bytes = Encoding.UTF8.GetBytes(s);
            Array.Resize(ref bytes, bytes.Length + 1);
            bytes[bytes.Length - 1] = 0; //The end of string '[=11=]' character
            GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
            _ptr =CvInvoke.cveStringCreateFromStr(handle.AddrOfPinnedObject());
            // ^ Exception is thrown ^
            handle.Free();
        }

        _needDispose = true;
    }

我已经尝试过将视频或图片导入到我的项目中,在启动项目时直接导入,或者在运行时使用OpenFileDialog选择文件,但都是一样的结果。一旦我选择一个文件并想将其加载到我的项目中,就会抛出异常。

编辑:已添加堆栈跟踪

堆栈跟踪

No suitable directory found to load unmanaged modules
Exception thrown : "System.DllNotFoundException" in Emgu.CV.dll
Exception thrown : "System.TypeInitializationException" in Emgu.CV.dll
Thread 0xa64 ended with Code 0 (0x0).
Exception thrown : "System.TypeInitializationException" in Emgu.CV.dll
Exception thrown : "System.TypeInitializationException" in Emgu.CV.dll
Exception thrown : "System.TypeInitializationException" in Emgu.CV.dll

我遇到过一次同样的问题,不知道对你有没有帮助,试试吧

只需将 cvexternmsvcp120msvcr120opencv_ffmpeg300_64 dll 拖放到您的项目中。它们应该位于 emgu...\bin\x64\.

这应该可以解决您的 DllNotFoundException,也许还有其他问题。