内容
C++代码
extern "C" _declspec(dllexport) void Getmat(uchar *data,size_t &size)
{
vector<uchar>buf;
Mat img = imread("D:\\1.jpg");//读入图片
imencode(".bmp", img, buf); //将Mat以BMP格式存入uchar的buf数组中
size = buf.size();
for each(uchar var in buf) //将buf拷贝到C#的byte[]中
{
*data = var;
data++;
}
}
C#代码
[DllImport("mydll.dll")]
public static extern void Getmat(ref byte data,out ulong size);
public Form1()
{
InitializeComponent();
byte[] imgdata = new byte[3200 * 2400 * 3];//存储图片的数组,必须大于等于图片分辨率
ulong size = new ulong();
Getmat(ref imgdata[0], out size);
pictureBox1.Image = Image.FromStream(new MemoryStream(imgdata, 0, (int)size));
}