1.新建项目
2.源文件---右键 添加---新建项(.cpp) / 头文件---右键 添加---新建项(.h)
3.配置opencv3.1
4..cpp文件中放入自己的程序(只是一个方法,或者一个类,不要main),并且在函数前加如下代码:
extern "C" __declspec(dllexport)
#include "stdio.h"
#include "opencv2/highgui/highgui_c.h"
#include <opencv2/opencv.hpp>
#include <tchar.h>
#include <fstream>
#include <string>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
using namespace cv;
// a.cpp : 定义 DLL 应用程序的导出函数。
extern "C" __declspec(dllexport)bool CutPic(char* path)
{
//代码段......
}
5..h文件中放入修改后的代码如下(请参考修改)(注:PACKAGEDLL是自己创建的文件名称大写):
#ifdef PACKAGEDLL_EXPORTS
#define PACKAGEDLL_API __declspec(dllexport)
#else
#define PACKAGEDLL_API __declspec(dllimport)
#endif
#ifdef PACKAGEDLL_EXPORTS
#define PACKAGEDLL_API extern "C" __declspec(dllexport)
#else
#define v extern "C" __declspec(dllimport)
#endif
PACKAGEDLL_API bool CutPic(char* path);
6.配置clr
7.生成---生成***(U)(注意:自己选择的模式,debug模式下编译的dll,只能debug模式下使用)
8.在输出中可以看见输出的路径
9.在C#程序中引入dll
先将生成的dll和opencv的dll,放到项目启动路径xxx\bin\Debug
10.在需要引入dll的文件中加入如下:
using System.Runtime.InteropServices;
[DllImport("PackageDll.dll")]//Dll名
public static extern bool CutPic(string path);//我得方法CutPic
11. C#环境配置(按如图红框配置--不勾选)
注意:自己调用的名字不要写错,dll放错地方会报错找不到dll
参考链接:
https://blog.csdn.net/liyuqian199695/article/details/53525178
https://blog.csdn.net/xiaolifeidaoer/article/details/81232666