C#联合Halcon编程No1--加载图像并显示

界面为Winform

1、首先引用halcondotnet.dll,工具箱-->选择项-->浏览-->添加halcondotnet.dll,加入Halcon窗口控件;

2、编辑界面如下图:

C#联合Halcon编程No1--加载图像并显示

解决方案如下:(Form1为Main窗口) 

C#联合Halcon编程No1--加载图像并显示

 Form1代码:

using System;
using System.IO;
using System.Windows.Forms;
using HalconDotNet;
using ReadImg.UI;

namespace ReadImg
{
    public partial class Form1 : Form
    {
        private string imgPath;
        private string filePath;
        HWindow win = null;//定义一个Halcon窗口对象
        HObject ho_Image = null;
        HTuple hv_Information = new HTuple();
        HTuple hv_ValueList = new HTuple(), hv_AcqHandle = new HTuple();
        HTuple hv_AcqHandle1 = new HTuple();
        HTuple width, height;
        OpenImg openImage;

        

        public Form1()
        {
            InitializeComponent();
            win = hWindowControl1.HalconWindow;//将Halcon窗口对象与控件进行关联
        }

        private void btn2DCode_Click(object sender, EventArgs e)
        {
            Form2DCode fm2d = new Form2DCode();
            fm2d.ShowDialog();
        }

        private void btnopenImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c://";
            openFileDialog.Filter = "BMP|*.bmp|JPG|*.jpg|PNG|*.png";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                imgPath = openFileDialog.FileName;
                this.txtBoxPath.Text = imgPath;
                filePath= Path.GetDirectoryName(openFileDialog.FileName)+"\\";//路径
            }
            if (ho_Image!=null)
            {
                ho_Image.Dispose();
            }
            
            openImage = new OpenImg();
            openImage.InfoFramegrabber(filePath, out hv_Information, out hv_ValueList, out hv_AcqHandle);
            HOperatorSet.ReadImage(out ho_Image, imgPath);
            HOperatorSet.GetImageSize(ho_Image,out width, out height);//获取图像尺寸
            win.SetPart(0, 0, (int)height - 1, (int)width - 1);//设置Halcon控件中图像的显示尺寸
            win.DispObj(ho_Image);//显示图像

        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (ho_Image != null)
            {
                ho_Image.Dispose();
            }
            openImage.ReadImage(hv_AcqHandle, out ho_Image);
            HOperatorSet.GetImageSize(ho_Image, out width, out height);//获取图像尺寸
            win.SetPart(0, 0, (int)height - 1, (int)width - 1);//设置Halcon控件中图像的显示尺寸
            win.DispObj(ho_Image);//显示图像
        }
    }
}


OpenImg类的代码如下:

using HalconDotNet;

namespace ReadImg
{
    class  OpenImg
    {
        
        public void InfoFramegrabber(string path,out HTuple hv_Information, out HTuple hv_ValueList, out HTuple hv_AcqHandle)
        {
            HOperatorSet.InfoFramegrabber("File", "general", out hv_Information, out hv_ValueList);
            HOperatorSet.OpenFramegrabber("File", 1, 1, 0, 0, 0, 0, "default", -1, "default",
                -1, "default", path, "default", -1, 1, out hv_AcqHandle);

        }
        public void ReadImage(HTuple hv_AcqHandle, out HObject ho_Image)
        {
            HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);//从文件夹中第一张图片开始读
        }

    }
}