C#联合halcon采集图像,用的笔记本摄像头

首先在halcon中采集笔记本摄像头插入代码。

C#联合halcon采集图像,用的笔记本摄像头

导出为C#格式的代码。

打开vs,配置环境,引用控件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;
using HalconDotNet;

namespace 采集图像
{
    public partial class Form1 : Form
    {
        HTuple hv_AcqHandle = null, hv_WindowHandle = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            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);
            HOperatorSet.OpenWindow(0, 0, this.pictureBox1.Width, this.pictureBox1.Height, this.pictureBox1.Handle, "visible", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
        }

        private void btnCaiji_Click(object sender, EventArgs e)
        {
            HObject ho_image = null;
            HTuple hv_Height = null; HTuple hv_Width = null;
            HOperatorSet.GrabImageAsync(out ho_image, hv_AcqHandle, -1);
            HOperatorSet.GetImageSize(ho_image, out hv_Width, out hv_Height);
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Height, hv_Width);
            HOperatorSet.DispObj(ho_image, hWindowControl1.HalconWindow);
            HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
            HOperatorSet.DispObj(ho_image, hv_WindowHandle);
            ho_image.Dispose();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);

        }
    }
}
这里我用了两个图像控件,分别为  pictureBox1和hv_WindowHandle控件。然后运行结果如下;

C#联合halcon采集图像,用的笔记本摄像头