C#联合halcon编写一个程序

使用软件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);
        }
    }
}

第八步:即可实现如下效果。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值