使用软件Visual Studio 2022
博客内容
1.C#调用halcon dll的过程
2.如何写一个测试程序证明C#halcon联合开发成功
第一步:找到halcon的bin目录下的halcon.dill或halcon.dll文件
第二步:找到bin文件目录下的dotnet35的所有文件
第三步:创建一个Windows窗体应用
第四步:将前两步的文件复制到创建文件的Debug目录下
第五步:添加其中的halcondotnet.dill到引用中即可
、
第六步:再将halcondotnet.dill复制到工具箱中即可出现窗体选项,这里选择HWindowControl进行使用。
第七步:添加好上图的按钮后,编写代码
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 第一个联合项目
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 图片的成员变量
/// </summary>
private HObject ho_image;
private void button_readImage_Click(object sender, EventArgs e)
{
HOperatorSet.ReadImage(out ho_image, "C:\\Users\\86152\\Desktop\\C#\\周测2\\周测2\\bin\\Debug\\image\\533bdf1504f01791c4c4851886d1a50d.jpeg");
HTuple width, height;
//获得图片的宽高
HOperatorSet.GetImageSize(ho_image,out width, out height);
//设置显示范围
//HWindowControl.HalconWindow -->控件的句柄 设置显示范围
HOperatorSet.SetPart(hWindowControl_yuanshi.HalconWindow,0,0, (height - 1),(width -1));
//显示
HOperatorSet.DispObj(ho_image, hWindowControl_yuanshi.HalconWindow);
}
HTuple MinGray;
HTuple MaxGray;
private void button_thresholdImage_Click(object sender, EventArgs e)
{
hWindowControls_threshold.HalconWindow.ClearWindow();
if (ho_image == null)
{
button_readImage_Click(null, null);
}
//设置颜色
HOperatorSet.SetColor(hWindowControls_threshold.HalconWindow,"red");
HObject Region;
//int.Parse(string) 字符串类型转换成int类型
MaxGray =int.Parse(textBox_MaxGray.Text);
MinGray = int.Parse(textBox_MinGray.Text);
//阈值分割
HOperatorSet.Threshold(ho_image, out Region, MinGray, MaxGray);
HTuple width, height;
HOperatorSet.GetImageSize(ho_image, out width, out height);
HWindowControl.HalconWindow -->窗体句柄 设置显示范围
HOperatorSet.SetPart(hWindowControls_threshold.HalconWindow, 0, 0, height - 1, width - 1);
显示
HOperatorSet.DispObj(Region, hWindowControls_threshold.HalconWindow);
}
}
}
第八步:即可实现如下效果。