C#与VisionPro联合开发——跳转页面

本文介绍了如何在Windows应用中,通过CognexVisionPro库实现页面间的跳转,包括打开相机预览和保存图片,以及使用ToolBlock组件。开发者展示了从Form1加载VPP文件、初始化相机和ToolBlock,并在FrmCamera和FrmTB页面间传递数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、跳转页面并打开相机

在这里插入图片描述
From1 所有代码展示

using System;
using System.IO;
using System.Windows.Forms;
//引入VisionPro命名空间
using Cognex.VisionPro;

namespace ConnectCamera {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
      
    CogAcqFifoTool fifoTool;  
    //窗口加载的Load事件
    private void Form1_Load(object sender, EventArgs e) {
  		//进行相机初始化(这里使用的是本地的vpp文件)
  		string path = Directory.GetCurrentDirectory() + "\\abc.vpp";
  		try {
    		//根据路径读取到vpp文件,初始化fifo对象
    		CogAcqFifoTool fifo = (CogAcqFifoTool)CogSerializer.LoadObjectFromFile(path);
    		if (fifo != null) {
      			MessageBox.Show("相机初始化成功!");
      			if (fifo.Operator.FrameGrabber != null) {
        			fifoTool = fifo;
      			}
    		}
  		} catch (Exception ex) {
    		MessageBox.Show("初始化失败:" + ex.Message);
  		}
	}
      
    //相机
    private void toolStripLabel1_Click(object sender, EventArgs e) {
      //传递到camera页面,这样取景工具在下一个窗体也可以访问到
      FrmCamera frm = new FrmCamera(fifoTool);
      frm.ShowDialog();
    }
  }
}

在这里插入图片描述

FrmCamera 代码展示

using System;
using System.Windows.Forms;
using Cognex.VisionPro;

namespace ConnectCamera {
  public partial class FrmCamera : Form {
    //声明 CogAcqFifoTool
    CogAcqFifoTool acq = null;
    VisionTool visionPro = new VisionTool();

    //方法里面传入参数
    public FrmCamera(CogAcqFifoTool fofo) {
      InitializeComponent();
      //显示的是上一个页面传入的图片
      cogAcqFifoEditV21.Subject = fofo;
    }

    //保存图像
    private void toolStripLabel1_Click(object sender, EventArgs e) {
      	string path = Directory.GetCurrentDirectory() + "\\ImageVpp";
		//没有文件夹则创建一个文件夹
		if (!Directory.Exists(path)) {
		  Directory.CreateDirectory(path);
		}
		try {
		  CogSerializer.SaveObjectToFile(cogAcqFifoEditV21.Subject, path + "\\" + DateTime.Now.ToString("HHmmss") + ".vpp");
		  MessageBox.Show("保存vpp成功!");
		} catch (Exception ex) {
		  MessageBox.Show(ex.Message);
		}
	}
  }
}

2、跳转页面并打开ToolBlock

From1 代码展示

using System;
using System.IO;
using System.Windows.Forms;
//引入VisionPro
using Cognex.VisionPro;

namespace ConnectCamera {
  public partial class Form1 : Form {
  public From1(){
       InitializeComponent();      
  }    
   CogToolBlock tb = null;
   
   //窗口的Load事件   
   private void Form1_Load(object sender, EventArgs e) {
      //进行相机初始化(这里使用的是本地的vpp文件)
      string path = Directory.GetCurrentDirectory() + "\\vpp\\tb.vpp";
	  tb = (CogToolBlock)CogSerializer.LoadObjectFromFile(path);
    }

    //打开FrmTB窗口
	private void btnFrmTB_Click(object sender, EventArgs e) {
  		FrmTB frmTB = new FrmTB(tb);
 		frmTB.ShowDialog();
	}
  }
}

FrmTB窗口
在这里插入图片描述
FrmTB 代码展示

using System.Windows.Forms;
using Cognex.VisionPro.ToolBlock;

namespace FrameGrabber {
  public partial class FrmTB : Form {
    CogToolBlock mTB = null;

    public FrmTB(CogToolBlock tb) {
      InitializeComponent();
      mTB = tb;
    }

    private void FrmTB_Load(object sender, System.EventArgs e) {
      //把读取到的ToolBlock赋值给当前控件
      cogToolBlockEditV21.Subject = mTB;
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值