HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);出现异常

Halcon C#联合编程中:HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);语句出现异常
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);出现异常_第1张图片
源码如下:

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 open_cam
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        HObject ho_Image = null;
        HTuple hv_AcqHandle = new HTuple(), hv_Width = new HTuple();
        HTuple hv_Height = new HTuple();
        private void button1_Click(object sender, EventArgs e)
        {
            HTuple hv_AcqHandle = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            //Image Acquisition 01: Code generated by Image Acquisition 01
            //Image Acquisition 01: Attention: The initialization may fail in case parameters need to
            //Image Acquisition 01: be set in a specific order (e.g., image resolution vs. offset).
            hv_AcqHandle.Dispose();
            HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                -1, "false", "default", "[0] Integrated Webcam", 0, -1, out hv_AcqHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "grab_timeout", 2000);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "gamma", 116);
            HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
        }
        HTuple hv_WindowHandle = new HTuple();
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);//open window,将图片的长宽改为控件的长宽
            HDevWindowStack.Push(hv_WindowHandle);//入栈
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            ho_Image.Dispose();
            HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
            //Image Acquisition 01: Do something
            hv_Width.Dispose(); hv_Height.Dispose();
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
            }
            
        }
    }
}

窗体界面:
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);出现异常_第2张图片
解决方法:
HTuple hv_AcqHandle = new HTuple()语句在全局声明以后又在按钮一的事件中声明,将按钮一中该语句删除即可。

你可能感兴趣的:(Halcon,视觉检测,图像处理,计算机视觉,c#)