C#联合Halcon相机实时采集

C#联合Halcon相机实时采集

1、界面设置
C#联合Halcon相机实时采集_第1张图片
2、打开相机

        /// 
        /// 打开相机
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            HOperatorSet.GenEmptyObj(out ho_Image);
            hv_AcqHandle.Dispose();
            HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                -1, "false", "default", "[1] ", 0, -1, out hv_AcqHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "grab_timeout", 5000);
            HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
        }

3、采集与显示

        /// 
        /// 采集显示
        /// 
        /// 
        /// 
        private void button2_Click(object sender, EventArgs e)
        {
            //启动定时器
            this.timer1.Enabled = true;

            //打开窗口,控件的宽,高,句柄
            HOperatorSet.OpenWindow(0, 0, this.hWindowControl1.Width, this.hWindowControl1.Height, this.hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);

        }

        /// 
        /// 定时器采集
        /// 
        /// 
        /// 
        private void timer1_Tick(object sender, EventArgs e)
        {
            //采集
            ho_Image.Dispose();
            HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
            //为了把图片显示全
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            if (HDevWindowStack.IsOpen())
            {
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height - 1, hv_Width - 1);
                }
            }
            //显示
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
        }

4、结果
C#联合Halcon相机实时采集_第2张图片

你可能感兴趣的:(C#联合Halcon,c#)