第一步
打开VS2017开发工具=>创建一个Windows窗体应用项目
创建的新项目如下图
第二步
选择"工具"菜单=>"选择工具箱项"
弹出"选择工具箱项"界面=>选择".NET Framework组件"=>单击"浏览"按钮
找到HALCON安装目录下"dotnet35"文件夹=>选中"halcondotnet.dll"=>单击"打开"按钮
返回到"选择工具箱项"界面=>勾选"HSmartWindowControl"和"HWindowControl"=>单击"确定"按钮
打开"工具箱"=>展开"常规"页签=>可以看到"HSmartWindowControl"和"HWindowControl"控件
第三步
打开解决方案资源管理器=>找到项目下的"引用"页签=>右键单击"引用"页签=>选择"添加引用"
弹出"引用管理器"界面=>选中"浏览"页签=>单击"浏览"按钮
找到HALCON安装目录下"dotnet35"文件夹=>选中"halcondotnet.dll"=>单击"添加"按钮
回到"引用管理器"界面=>勾选"halcondotnet.dll"=>单击"确定"按钮
回到"解决方案资源管理器"界面=>在项目下的"引用"中出现"halcondotnet"
在代码编辑窗口写入"using HalconDotNet;"
拖入"HWindowControl"控件到界面
在代码窗口编辑以下一撮代码
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;using HalconDotNet;namespace TestHalconNet{ public partial class Form1 : Form { bool isCameraStatus; public HTuple hv_AcqHandle; public HTuple hv_ExpDefaultWinHandle; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string MessageStr = string.Empty; //打开相机 if (!isCameraStatus) { if (Opencamera(hWindowControlMain.HalconWindow, out MessageStr)) { } else { MessageBox.Show(MessageStr, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private bool Opencamera(HTuple Window, out string MessageStr) { MessageStr = string.Empty; HTuple hv_Width = new HTuple(); HTuple hv_Height = new HTuple(); HObject ho_Image; HOperatorSet.GenEmptyObj(out ho_Image); try { hv_ExpDefaultWinHandle = Window; hv_AcqHandle = new HTuple(); hv_AcqHandle.Dispose(); HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false", "default", "[0] ", 0, -1, out hv_AcqHandle); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); ho_Image.Dispose(); HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); hv_Width.Dispose(); hv_Height.Dispose(); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill"); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); isCameraStatus = true; return true; } catch (Exception ex) { MessageStr = ex.Message; MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } finally { ho_Image.Dispose(); hv_Width.Dispose(); hv_Height.Dispose(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { string MessageStr = string.Empty; //关闭相机 if (isCameraStatus) { if (Closecamera(hWindowControlMain.HalconWindow, out MessageStr)) { } else { MessageBox.Show(MessageStr, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } public bool Closecamera(HTuple Window, out string MessageStr) { MessageStr = string.Empty; try { HOperatorSet.CloseFramegrabber(hv_AcqHandle); HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle); isCameraStatus = false; return true; } catch (Exception ex) { MessageStr = ex.Message; MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } finally { hv_AcqHandle.Dispose(); } } }}
平台选择"x64"=>单击"启动"按钮运行程序
如下是程序正常运行后使用笔记本电脑摄像头拍摄的图片