金橙子激光打标机的二次开发(C#)

软件需求

客户要求提供一个简单的界面,使操作人员只需要设置批次与流水号,余的部分如日期二维号要根据他们提供的规则在规则的时间自动生成和切换,因为规则比较特殊,比如他们的日期要用特定的字母表示,这样的话只用ezcad是无法实现的,只能进行二次开发。

关于MarkEzd.dll

MarkEzd.dll是金橙子公司提供的ezcad的sdk,ezcad都许多版本,目前了解到了9个,每个版本都有对应的MarkEzd.dll,不能通用,官网不提供下载,说明书也只能找到很老的一个版本,在找到的示例中发现有新函数加入,但说明书上看不到。

C#调用MarkEzd.dll


        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_Initial2", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern ERRCODE Lmc1_Initial(string strEzCadPath, bool bTestMode);

        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_GetEntityCount", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern int Lmc1_GetEntityCount();
        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_GetEntityName", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern ERRCODE Lmc1_GetEntityName(int nEntityIndex, string szEntName);

        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_Close", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern ERRCODE Lmc1_Close();

        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_LoadEzdFile", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern ERRCODE Lmc1_LoadEzdFile(string strFileName);

        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_Mark", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern ERRCODE Lmc1_Mark(bool bFlyMark);

        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_ChangeTextByName", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern ERRCODE Lmc1_ChangeTextByName(string strTextName, string strTextNew);

        [DllImport("MarkEzd.dll", EntryPoint = "lmc1_GetPrevBitmap2", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr Lmc1_GetPrevBitmap(int nBMPWIDTH, int nBMPHEIGHT);

调用流程

初始化>载入ezd文件>标刻

           string _strEzCadPath = "";                                      //存储ezcad.exe所在的目录的全路径名称
           bool _bTestMode = false;                                         //是否为测试模式
           dwErrCde = Lmc1_Initial(_strEzCadPath, _bTestMode);             //设备初始化
           string _strFileName = "15AH.ezd";                               //指定的ezd模板文件
           dwErrCde = Lmc1_LoadEzdFile(_strFileName);                      //加载打印模板
           dwErrCde = Lmc1_Mark(false);  //标刻

其它功能1. 根据名称修改模板内容

        _strTextName = "SN";//要更改内容的文本对象的名称
        _strTextNew = textBox1.Text;//新的文本内容           
        dwErrCde = Lmc1_ChangeTextByName(_strTextName, _strTextNew);//填充模板的文本内容

其它功能2. 获取预览图像

此处介绍的是新方法,老方法需要自己用C++编译一个CBitmap转HBitmap的库,新方法直接返回HBitmap

		IntPtr imagePtr = new IntPtr();
        IntPtr imagePtrNew = new IntPtr();
        private Image getImage(int width,int height)
        {
           Bitmap image = new Bitmap(width, height);
            imagePtr = Lmc1_GetPrevBitmap(width, height);
            if (imagePtr != imagePtrNew)
                image=Image.FromHbitmap(imagePtr);
            return image;
        }

补充 内存释放

客户反应,软件长时间运行会卡死,经过测试,每调用一次 Lmc1_GetPrevBitmap,系统使用中的内存就会增加,4000次+,8G内存就会耗尽,9945次左右,软件就会崩溃!最终被我找到内存释放的方法。

        [System.Runtime.InteropServices.DllImport("gdi32.dll", SetLastError = true)]
        private static extern bool DeleteObject(IntPtr hObject);
		IntPtr imagePtr = new IntPtr();
        IntPtr imagePtrNew = new IntPtr();
        private Image getImage(int width,int height)
        {
           Bitmap image = new Bitmap(width, height);
            imagePtr = Lmc1_GetPrevBitmap(width, height);
            if (imagePtr != imagePtrNew)
                image=Image.FromHbitmap(imagePtr);
            DeleteObject(imagePtr);
            return image;
        }

你可能感兴趣的:(Csharp)