一种c#代码模式

ImageProvider.cs

public delegate void ImageReadyEventHandler();
public event ImageReadyEventHandler ImageReadyEvent;
protected void OnImageReadyEvent()
      {
          if (ImageReadyEvent != null)
          {
              ImageReadyEvent();
          }
      }
      
下面为调用OnImageReadyEvent()的地方
 protected void Grab()
        {
            /* Notify that grabbing has started. This event can be used to update the state of the GUI. */
            OnGrabbingStartedEvent();
            try
            {
                /* Set up everything needed for grabbing. */
                SetupGrab();

                while (m_grabThreadRun) /* Is set to false when stopping to end the grab thread. */
                {
                    /* Wait for the next buffer to be filled. Wait up to 1000 ms. */
                    if (!Pylon.WaitObjectWait(m_hWait, 1000))
                    {
                        lock (m_lockObject)
                        {
                            if (m_grabbedBuffers.Count != m_numberOfBuffersUsed)
                            {
                                /* Timeout occurred. */
                                //throw new Exception("A grab timeout occurred.");
                                //Close();
                            }
                            continue;
                        }
                    }

                    PylonGrabResult_t grabResult; /* Stores the result of a grab operation. */
                    /* Since the wait operation was successful, the result of at least one grab 
                       operation is available. Retrieve it. */
                    if (!Pylon.StreamGrabberRetrieveResult(m_hGrabber, out grabResult))
                    {
                        /* Oops. No grab result available? We should never have reached this point. 
                           Since the wait operation above returned without a timeout, a grab result 
                           should be available. */
                        throw new Exception("Failed to retrieve a grab result.");
                    }

                    /* Check to see if the image was grabbed successfully. */
                    if (grabResult.Status == EPylonGrabStatus.Grabbed)
                    {
                        /* Add result to the ready list. */
                        EnqueueTakenImage(grabResult);

                        /* Notify that an image has been added to the output queue. The receiver of the event can use GetCurrentImage() to acquire and process the image 
                         and ReleaseImage() to remove the image from the queue and return it to the stream grabber.*/
                        OnImageReadyEvent();        //在这里调用

                        /* Exit here for single frame mode. */
                        if (m_grabOnce)
                        {
                            m_grabThreadRun = false;
                            break;
                        }
                    }
                    else if (grabResult.Status == EPylonGrabStatus.Failed)
                    {
                        /* 
                            Grabbing an image can fail if the used network hardware, i.e. network adapter, 
                            switch or Ethernet cable, experiences performance problems.
                            Increase the Inter-Packet Delay to reduce the required bandwidth.
                            It is recommended to enable Jumbo Frames on the network adapter and switch.
                            Adjust the Packet Size on the camera to the highest supported frame size.
                            If this did not resolve the problem, check if the recommended hardware is used.
                            Aggressive power saving settings for the CPU can also cause the image grab to fail.
                        */
                        throw new Exception(string.Format("A grab failure occurred. See the method ImageProvider::Grab for more information. The error code is {0:X08}.", grabResult.ErrorCode));
                    }
                }

                /* Tear down everything needed for grabbing. */
                CleanUpGrab();
            }
            catch (Exception e)
            {
                /* The grabbing stops due to an error. Set m_grabThreadRun to false to avoid that any more buffers are queued for grabbing. */
                m_grabThreadRun = false;

                /* Get the last error message here, because it could be overwritten by cleaning up. */
                string lastErrorMessage = GetLastErrorText();

                try
                {
                    /* Try to tear down everything needed for grabbing. */
                    CleanUpGrab();
                }
                catch
                {
                    /* Another exception cannot be handled. */
                }

                /* Notify that grabbing has stopped. This event could be used to update the state of the GUI. */
                OnGrabbingStoppedEvent();

                if (!m_removed) /* In case the device was removed from the PC suppress the notification. */
                {
                    /* Notify that the grabbing had errors and deliver the information. */
                    OnGrabErrorEvent(e, lastErrorMessage);
                }
                return;
            }
            /* Notify that grabbing has stopped. This event could be used to update the state of the GUI. */
            OnGrabbingStoppedEvent();
        }

在MainForm.cs

m_imageProvider.ImageReadyEvent += new ImageProvider.ImageReadyEventHandler(OnImageReadyEventCallback);
 private void OnImageReadyEventCallback()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ImageProvider.ImageReadyEventHandler(OnImageReadyEventCallback));
                return;
            }

            try
            {
                ImageProvider.Image image = m_imageProvider.GetLatestImage();

                if (image != null)
                {
                    if (BitmapFactory.IsCompatible(m_bitmap, image.Width, image.Height, image.Color))
                    {
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        pictureBox.Image = m_bitmap;
                    }
                    else 
                    {
                        BitmapFactory.CreateBitmap(out m_bitmap, image.Width, image.Height, image.Color);
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        Bitmap bitmap = pictureBox.Image as Bitmap;
                        pictureBox.Image = m_bitmap;
                        if (bitmap != null)
                        {
                          bitmap.Dispose();
                        }
                    }
                   // m_imageProvider.ReleaseImage();
                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }


转载于:https://my.oschina.net/dukson/blog/270290

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值