C# uses the VisionPro library in VS to read the camera list and capture images

The basic steps to open the camera and obtain images through VisionPro in the .NET environment are as follows:
1. Create a CogFrameGrabberGigEs object and obtain all camera devices connected to the frame grabber.
2. Obtain the reference of a single object ICogFrameGrabber through CogFrameGrabberGigEs. Supported image formats, camera serial numbers and other information can be obtained from this interface object.
3. Create the ICogAcqFifo interface object through the CreateAcqFifo method of CogFrameGrabber. When using the CreateAcqFifo method, you must specify the image format, camera port and other information.
4. The required image data can be obtained through the Acquire and other methods of the ICogAcqFifo interface.
5. Disconnect CogFrameGrabber from the hardware before exiting the program, otherwise it may cause an exit exception.

Notes:
① The IP of the camera and the network card are in the same network segment, the firewall is turned off, the network card's giant frames and other physical hardware parameters are set correctly.
②Select x64 as the compilation platform, otherwise even if there is no problem with the physical connection of the camera, the Count attribute of the created CogFrameGrabbers may be zero.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Cognex.VisionPro;

namespace ImageAcquire
{
    
    
        public class Form1 : System.Windows.Forms.Form
        {
    
    
                private Cognex.VisionPro.Display.CogDisplay cogDisplay1;//用于进行图像显示的VisionPro控件
                ICogAcqFifo myFifo = null;
                ICogFrameGrabber myFrameGrabber = null;
                public Form1()
                {
    
    
                        InitializeComponent();
                        InitializeAcquisition();
                }

                protected override void Dispose( bool disposing )
                {
    
    
                        if( disposing )
                        {
    
    
                                if (components != null)
                                {
    
    
                                        components.Dispose();
                                }
                                //5-断开CogFrameGrabber对象与硬件的连接。
                                if(myFrameGrabber!=null)
                                myFrameGrabber.Disconnect(false);
                        }
                        base.Dispose( disposing );
                }

                private void InitializeAcquisition()
                {
    
    
                        const string VIDEO_FORMAT = "Sony XC75 640x480";
                         // 1-创建CogFrameGrabbers对象
                        CogFrameGrabbers myFrameGrabbers = new CogFrameGrabbers(); 
                        //2-获取单个ICogFrameGrabber接口对象
                        myFrameGrabber = myFrameGrabbers[0];
                        //3-创建ICogAcqFifo接口对象
                        myFifo = myFrameGrabber.CreateAcqFifo(VIDEO_FORMAT,Cognex.VisionPro.CogAcqFifoPixelFormatConstants.Format8Grey, 0, false);
                }


                private void button1_Click(object sender, System.EventArgs e)
                {
    
    
                        int trigNum;
                        //4-通过ICogAcqFifo接口对象的Acquire方法进行图像采集。
                        cogDisplay1.Image = myFifo.Acquire(out trigNum);
                }
        }
}

Guess you like

Origin blog.csdn.net/weixin_45499836/article/details/124045630