C#调用C++生成的dll,参数有结构体数组

废话少说,只上程序。

C++生成dll程序:

/*
实现功能:将一个图片中,所在行的像素值,按照一定的间隔(列数),将对应点的像素值提取出来。
*/

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;
using namespace cv;

struct Node
{
	int R;
	int G;
	int B;
};

extern "C" _declspec(dllexport)  bool operator_Image(char *path , int rownum,int intervalNum,struct Node *Result,int *Num)
{
	Mat src = imread(path);
	int rows,cols;
	int x = 0,y = 0;
	//cout << src.row(100) << endl;
	rows = src.rows;
	cols = src.cols;
	if (src.empty())
	{
		cout << "图片不存在!" << endl;
	}
	if (rows < intervalNum)
	{
		cout << "你输入的间隔有误!!" << endl;
	}
	//创建动态数组,用来保存像素的值
	//Result = new int[cols/intervalNum*3*sizeof(int)];
	//int savedata[10000] = {0};

	*Num = cols/intervalNum*3;


	int i = 0;
	while(x<cols)
	{
			y = rownum;
			//Result[i++] = src.at<Vec3b>(y,x)[0];
			//Result[i++] = src.at<Vec3b>(y,x)[1];
			//Result[i++] = src.at<Vec3b>(y,x)[2];
			Result[i].G = src.at<Vec3b>(y,x)[0];
			Result[i].B = src.at<Vec3b>(y,x)[1];
			Result[i].R = src.at<Vec3b>(y,x)[2];
			/*if (x%100 == 0)
			{
			printf("%d ",src.at<Vec3b>(y,x)[0]);
			printf("%d ",src.at<Vec3b>(y,x)[1]);
			printf("%d ",src.at<Vec3b>(y,x)[2]);
			}*/
			x = x+intervalNum;	
			i++;
	}
	return true;

}

C#中调用他的程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public struct Node
    {
        int R;
        int G;
        int B;
    };

namespace TestLSPVC
{
     public partial class Form1 : Form
    {

        [DllImport("readcolpixelDll.dll", EntryPoint = "operator_Image", CharSet = CharSet.Ansi)]
        public static extern bool operator_Image(string path , int rownum,int intervalNum, ref  Node Result,ref int Num);


        public Form1()<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = "c:\\lena.jpg";
              Node[] x = new  Node[1000];
             int Count=0;
            operator_Image(path,100,100,ref x[0],ref Count);

        }
    }
}

以上程序,经过测试,没有问题。 大笑 大笑 大笑 大笑 大笑 大笑 大笑 大笑 大笑 大笑 大笑 大笑

DLL工程调试步骤:

1、dll工程>>属性>>配置属性>>生成>>选择目录文件夹为要调用该dll文件的工程的bin文件夹
2、在要调用该dll文件的工程中添加引用该dll(dll文件在该工程的bin文件夹中)
3、在dll工程中设置断点
4、按F5即可进入调试




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值