C#和Halcon混合编程
2018-01-01 本文已影响1125人
xigua1234
以一个简单的功能,打开图片,为例子
1.halcon代码
dev_close_window ()
read_image (Image, 'C:/Users/Public/Pictures/Sample Pictures/Koala.jpg')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
2.导出halcon代码(文件->导出)

3.建立C#工程

4.添加bin\dotnet35\halcondotnet.dll的引用

5.添加工具箱
找到工具箱,如果没有,就在顶部菜单--视图,中找到.
在空白处,右键--选择项,浏览,找到bin\dotnet35\halcondotnet.dll

这样操作之后啥也没有?
鼠标右键,勾选"全部显示"就行了.

6.设置界面
打开解决方案管理器,Form1.cs来设置界面

在工具箱中找到控件,直接往里面拖.

7.修改代码

点击事件代码:
private void btnReadImage_Click(object sender, EventArgs e)
{
// 增加代码
HDevelopExport HD = new HDevelopExport();
HD.RunHalcon(hWindowControl1.HalconWindow);
}
halcon导出的代码:
去掉下面的语句,粘贴到Form1.cs中namespace代码中
using System;
using HalconDotNet;
8.将botton控件和点击事件关联起来

在Click一栏,可以选择点击事件.选中
btnReadImage_Click
即可
9,运行

10.可能的错误
1)可能会出现C# 无法加载 DLL“halcon”: 找不到指定的模块
的问题,
找到halcon.dll,把它放在bin\release和bin\debug文件夹即可
2)试图加载格式不正确的程序
上面,咱们加载进来的dll都是64位的,所以debug/Release的设置都应该是64位平台
3)项目框架必须和halcondotnet.dll的框架相同,如果它是从halcon安装目录下,bin\dotnet35目录下拷贝过来的,显然框架不能高于.net3.5(经测试,设为4.0不一定会出错)