c# OpenCvSharp 无法加载 DLLOpenCvSharpExtern:找不到指定的模块
在C#中使用OpenCvSharp出现无法加载 DLLOpenCvSharpExtern:找不到指定的模块。
或者提示“OpenCvSharp.NativeMethods”的类型初始值设定项引发异常
如以下图片的提示:
而你的程序bin文件夹却有dll文件夹
还是无法正确的读取OpenCvSharpExtern
可在使用OpenCvSharp前调用如下代码
让库OpenCvSharpExtern.dll是从应用程序路径的x86或x64子目录动态加载
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)] private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName); #region 检查指针大小,让库OpenCvSharpExtern.dll是从应用程序路径的x86或x64子目录动态加载 public static bool TryLoadNativeLibrary(string path) { if (path == null) return false; path = Path.Combine(path, IntPtr.Size == 4 ? "x86" : "x64"); path = Path.Combine(path, "OpenCvSharpExtern.dll"); return File.Exists(path) && LoadLibrary(path) != IntPtr.Zero; } #endregion