1.在halcon中编写读取并显示图片代码
dev_close_window ()
read_image (Image, 'C:/Users/ThinkPad/Pictures/fengjing.png')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
dev_set_part (0, 0, Height-1,Width-1)
dev_display (Image)
2.导出halcon代码,转为c#代码
using System;
using HalconDotNet;
public partial class HDevelopExport
{
public HTuple hv_ExpDefaultWinHandle;
// Main procedure
private void action()
{
// Local iconic variables
HObject ho_Image;
// Local control variables
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
//dev_close_window(...);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, "C:/Users/ThinkPad/Pictures/fengjing.png");
hv_Width.Dispose();hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
//dev_open_window(...);
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height-1, hv_Width-1);
}
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
ho_Image.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
hv_WindowID.Dispose();
}
public void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
public void RunHalcon(HTuple Window)
{
hv_ExpDefaultWinHandle = Window;
action();
}
}
3.在vs中引入hancon的dll,步骤如下:
引用->鼠标右键->添加引用->浏览->halcon安装文件夹->bin->dotnet35->halcondotnet.dll->确定
4.添加工具箱,步骤如下:
选工具箱->常规->鼠标右键->选择项浏览->halcon安装文件夹->bin->dotnet35->halcondotnet.dll->确定
可以看到工具箱中,在windows窗体中看到如下图的控件。
5.设计界面
拖入HWindwcontrol控件和button控件
6.将代码填入相应位置
(1)引入相应的命名空间
using HalconDotNet;
(2)将导出的类代码填入from.cs类里面代码的下方,
(3)在butron按钮代码里面填入
HDevelopExport HD = new HDevelopExport();
HD.RunHalcon(hWindowControl1.HalconWindow);
(4)总代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using HalconDotNet;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void paizhao_Click(object sender, EventArgs e)
{
HDevelopExport HD = new HDevelopExport();
HD.RunHalcon(hWindowControl1.HalconWindow);
}
}
public partial class HDevelopExport
{
public HTuple hv_ExpDefaultWinHandle;
// Main procedure
private void action()
{
// Local iconic variables
HObject ho_Image;
// Local control variables
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
//dev_close_window(...);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, "C:/Users/ThinkPad/Pictures/fengjing.png");
hv_Width.Dispose(); hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
//dev_open_window(...);
using (HDevDisposeHelper dh = new HDevDisposeHelper())
{
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_Height - 1, hv_Width - 1);
}
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
ho_Image.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
// hv_WindowID.Dispose();
}
public void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
public void RunHalcon(HTuple Window)
{
hv_ExpDefaultWinHandle = Window;
action();
}
}
}
7.运行情况如下