C#调用C++ Dll

来源:互联网 发布:淘宝售后率怎么算 编辑:程序博客网 时间:2024/06/06 07:50

如果想在C#中调用C++的dll,按照以下步骤操作即可:

(1)在C#程序中加入导入语句,如下:

         [DllImport("CVocrModelSDKClr.dll", EntryPoint = "Init", CallingConvention = CallingConvention.Cdecl)]
         public static extern int Init(string libPath, int imageWidth, int imageHeight);

(2)将dll拷贝到C#的exe目录;


以下是示例代码:

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.Collections;
using System.Runtime.InteropServices;


namespace CSharpTestSDK
{
    unsafe public struct CSubtitleBlockInfo
    {
        public int fontIndex;
        public int subtitleRect_L;
        public int subtitleRect_T;
        public int subtitleRect_R;
        public int subtitleRect_B;
        public int foreColor_R;
        public int foreColor_G;
        public int foreColor_B;
        public int backgroundColor_R;
        public int backgroundColor_G;
        public int backgroundColor_B;
        public int characterHeight;
        public int characterWidth;
    };
    unsafe public struct CSubtitleBlockInfoSet
    {
        public CSubtitleBlockInfo* subtitleBlockInfo;
        public int count;
        public int capacity;
    };
    unsafe public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        [DllImport("CVocrModelSDKClr.dll", EntryPoint = "Init", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Init(string libPath, int imageWidth, int imageHeight);


        [DllImport("CVocrModelSDKClr.dll", EntryPoint = "GetSubtitleInfo", CallingConvention = CallingConvention.Cdecl)]
        public static extern int GetSubtitleInfo(int[] aImageData, int aImageSize, ref CSubtitleBlockInfoSet aSubtitleBlockInfoSet);


        [DllImport("CVocrModelSDKClr.dll", EntryPoint = "ReleaseResource", CallingConvention = CallingConvention.Cdecl)]
        public static extern int ReleaseResource();


        private void button1_Click(object sender, EventArgs e)
        {
            int ret;
            string rootPath;
            int imageWidth = 0, imageHeight = 0, topRowNum = 0;
            Image img = Image.FromFile("f:/21.jpg");
            Bitmap btp = (Bitmap)img;
            imageWidth = img.Width;
            imageHeight = img.Height / 3;
            topRowNum = img.Height - imageHeight;
            int[] imageData = new int[imageWidth*imageHeight*3];
            CSubtitleBlockInfoSet subtitleBlockInfoSet;
            subtitleBlockInfoSet.capacity = 0;
            subtitleBlockInfoSet.count = 0;
            subtitleBlockInfoSet.subtitleBlockInfo = null;
            for (int i = 0; i < imageHeight; i++)
            {
                for (int j = 0; j < imageWidth; j++ )
                {
                    imageData[i * imageWidth * 3 + 3 * j] = btp.GetPixel(j, i + topRowNum).R;
                    imageData[i * imageWidth * 3 + 3 * j + 1] = btp.GetPixel(j, i + topRowNum).G;
                    imageData[i * imageWidth * 3 + 3 * j + 2] = btp.GetPixel(j, i + topRowNum).B;
                }
            }


            //
            rootPath = System.Environment.CurrentDirectory;
            ret = Init(rootPath, imageWidth, imageHeight);
            if (ret == 0)
            {
                MessageBox.Show("内部错误");
            }
            ret = GetSubtitleInfo(imageData, imageWidth*imageHeight, ref subtitleBlockInfoSet);
            if (ret == 0)
            {
                MessageBox.Show("内部错误");
            }
            for (int i = 0; i < subtitleBlockInfoSet.count;  i++ )
            {
                subtitleBlockInfoSet.subtitleBlockInfo[i].subtitleRect_T += imageHeight;
                subtitleBlockInfoSet.subtitleBlockInfo[i].subtitleRect_B += imageHeight;
            }
            //ReleaseResource();
        }
    }
}
0 0
原创粉丝点击