1、跳转页面并打开相机
From1 所有代码展示
csharp
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 代码展示
csharp
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) {
visionPro.fifoTool = cogAcqFifoEditV21.Subject;
//提供保存vpp的方法
visionPro.SaveVpp(cogAcqFifoEditV21.Subject);
}
}
}
2、跳转页面并打开ToolBlock
From1 代码展示
csharp
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 代码展示
csharp
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;
}
}
}