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

慈云数据 2024-03-12 技术支持 121 0

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;
    }
  }
}
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon