c#halcon使用线程抓取图片

前言

提示:这里可以添加本文要记录的大概内容:
按照之前的经验,单线程取图需要用timer实时抓,有时候会出现线程卡住的情况。另外同样程序使用单线好像比多线内存用的大,这个还得再看看。


提示:以下是本篇文章正文内容,下面案例可供参考

一、项目条件

使用c#和halcon联合,利用多线程的方法,采集图像按比例放到开的hwindowcontrol窗中,checkbox按钮控制实时采集

二、使用步骤

1.引入

代码如下(示例):

using 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 System.Threading;
namespace 多线程抓取图像
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
        HTuple hv_AcqHandle;
        HObject ho_Image = null;
        HTuple hv_Width, hv_Height;
        bool fContiGrab;
        Thread thContiGrab;
        HTuple hv_WindowHandle;

        private void chkcontinueGrab_CheckedChanged(object sender, EventArgs e)
        {
            if(chkcontinueGrab.CheckState==CheckState.Checked)
            {
                fContiGrab = true;
                //开线程
                thContiGrab = new Thread(new ThreadStart(ThreadContinueGrab));
                thContiGrab.Start();
                thContiGrab.IsBackground = true;
            }
            else
            {
                fContiGrab = false;
            }
        }
        private void ThreadContinueGrab()
        {
            while(fContiGrab)
            {
                HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
                HOperatorSet.DispObj(ho_Image, hv_WindowHandle);
                ho_Image.Dispose();
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                -1, "false", "default", "[0] USB2.0 VGA UVC WebCam", 0, -1, out hv_AcqHandle); HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
            HOperatorSet.OpenWindow(0, 0, hWindowControl1.Size.Width, hWindowControl1.Size.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
        }


    }
}

总结

1.需要在方案中新增dll的halcondotnet文件
2.工具添加项也需要增加该dll
3.需要引用该文件
4.需要引用系统中的线程

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锋利成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值