AVT c# 连续采集

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using AlliedVisionTech;
using AlliedVisionTech.UniFoundationNet;
using AlliedVisionTech.UniControlNet;
using AlliedVisionTech.UniTransformNet;
using System.Threading;
namespace MicroView.Net
{

    public partial class MicroViewForm : Form
    {
        UniCamera[] cams = null;
        Mutex m_TreeLock = new Mutex();
        /// <summary>
        /// delegate method for a node list  change event from api
        /// </summary>
        /// <remarks>
        /// dont forget to test InvokeRequired, because the calling context is different from the GUI
        /// </remarks>

        private void OnNodeListChanged()
        {
            if (InvokeRequired) // if not from this thread invoke it in our context
            {
                Invoke( new UniControl.OnNodelistChangeHandler( OnNodeListChanged) );
                return;
            }
            m_TreeLock.WaitOne();                               // protect the tree from multi access
            cams = UniControl.GetCameras();                     // get all found cameras from UniControl
            CameraTree.BeginUpdate();                           // disable refresh
            CameraTree.Nodes.Clear();                           // clear tree
            TreeNode root = CameraTree.Nodes.Add("Cameras");    // root node for tree
            if (null == root)                                   // handle error
            {
                MessageBox.Show("no Memory for node");
                return;
            }
            root.Tag = null;                                    // set tag to meaningless
            for (UInt32 i = 0; i < cams.Length; ++i)            // for every found camera
            {
                try
                {
                    cams[i].Open();                                 // open camera and attach to tree
                    TreeNode node = root.Nodes.Add(cams[i].VendorName + " " + cams[i].ModelName + " (" + cams[i].SerialNumber.ToString() + ")");
                    node.Tag = cams[i];                             // ready, so close the camera
                    cams[i].Close();
                }
                catch (UniControlException uce) // handle camera open fail
                {
                    MessageBox.Show(uce.ToString() );
                }
            }
            CameraTree.EndUpdate();                                 // ready with the tree update, so refresh
            m_TreeLock.ReleaseMutex();                              // and free the resource
        }
        /// <summary>
        /// default Ctor
        /// </summary>
        public MicroViewForm()
        {
            InitializeComponent();  // setup gui classes
            // the first thing to do, Init the Api
            UniControl.Init();
            // now all calls are valid.
            DisplayPicture.SizeMode = PictureBoxSizeMode.StretchImage;                                  // the image should stretch itself into the control
            UniControl.OnNodeListChanged += new UniControl.OnNodelistChangeHandler(OnNodeListChanged);  // register the node list  changed handler
        }
        /// <summary>
        /// this is called from framework then the form hase closed
        /// </summary>
        /// <param name="sender"> ignored</param>
        /// <param name="e"> ignored</param>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            UniControl.Release();   // when the form closes we free the api
        }

        /// <summary>
        /// emitted when the camera tree is double clicked.
        /// </summary>
        /// <param name="sender"> can only be the CameraTree</param>
        /// <param name="e">ignored</param>
        private void OnCameraClicked(object sender, EventArgs e)
        {
            TreeNode current = CameraTree.SelectedNode; // get the clicked node
            if (null != current && null != current.Tag) // was a node selected and is the Tag not empty
            {
                UniCamera cam = (UniCamera)current.Tag;             // try  casting the Tag to a UniCamera
                if (null == cam)                                    // did it work?
                {
                    MessageBox.Show("no camera in selected item");
                    return;
                }
                try
                {
                    cam.Open();                                                                 // open the camera
                    UInt32 Width = cam.CurImageWidth;                                           // get current width from camera
                    UInt32 Height = cam.CurImageHeight;                                         // get current height from camera
                    enUniColorCode color_code = cam.CurColorCodeEn;                             // current IIDC color code
                    UInt32 dcam_mode = 0;                                                       // we want Format/ Mode 0
                    cam.PrepareFreeGrab(ref dcam_mode, ref color_code, ref Width, ref Height);  // try preparing camera for free grab
                    PixelFormat pixel_format = cam.GrabPixelFormat();                           // get the PixxelFormat for the current mode
                    Bitmap image = new Bitmap((int)Width, (int)Height, pixel_format);           // create an matching image
                    OutPut.Text = cam.SerialNumber.ToString() +" (" + Width.ToString() + "x" + Height.ToString() + ") " + pixel_format.ToString();
                    cam.GrabImage(image, 3000);                                                 // grab the image
                    cam.Close();
                    if (PixelFormat.Format8bppIndexed == pixel_format)                          // if its 8 bit we need a palette
                    {
                        UniTransform.MakeGrayScalePalette(image);
                    }
                    DisplayPicture.Image = image;                                               // put it into the display container
                }
                catch( UniControlException uce)                                                 // handle errors
                {
                    MessageBox.Show(uce.ToString() );
                }
            }
        }

        /// <summary>
        /// called when the form is first shown
        /// </summary>
        private void OnFirstShown(object sender, EventArgs e)
        {
            OnNodeListChanged();        // the initial node listing
        }
    }
}

posted on 2012-04-10 11:20  小楼-machine -vision  阅读(470)  评论(0)    收藏  举报