Ejemplo n.º 1
0
        /* Handles the event related to an image having been taken and waiting for processing. */
        private void OnImageReadyEventCallback()
        {
            if (InvokeRequired)
            {
                /* Suspend the grab thread for a while to avoid blocking the computer by using up all processor resources. */
                System.Threading.Thread.Sleep(20); /* This is only required for this sample. */
                /* If called from a different thread, we must use the Invoke method to marshal the call to the proper thread. */
                BeginInvoke(new ImageProvider.ImageReadyEventHandler(OnImageReadyEventCallback));
                return;
            }

            try
            {
                /* Acquire the image from the image provider. */
                ImageProvider.Image image = m_imageProvider.GetCurrentImage();

                /* Check if the image has been removed in the meantime. */
                if (image != null)
                {
                    /* Check if the image is compatible with the currently used bitmap. */
                    if (BitmapFactory.IsCompatible(m_bitmap, image.Width, image.Height, image.Color))
                    {
                        /* Update the bitmap with the image data. */
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* To show the new image, request the display control to update itself. */
                        pictureBox.Refresh();
                    }
                    else /* A new bitmap is required. */
                    {
                        BitmapFactory.CreateBitmap(ref m_bitmap, image.Width, image.Height, image.Color);
                        BitmapFactory.UpdateBitmap(m_bitmap, image.Buffer, image.Width, image.Height, image.Color);
                        /* Provide the display control with the new bitmap. This action automatically updates the display. */
                        pictureBox.Image = m_bitmap;
                    }
                    if (is_recording == true)
                    {
                        RotateFlipType t = RotateFlipType.RotateNoneFlipY;
                        m_bitmap.RotateFlip(t);
                        aw.AddFrame(m_bitmap);
                    }

                    /* The processing of the image is done. Release the image buffer. */
                    m_imageProvider.ReleaseImage();

                    /* The buffer can be used for the next image grabs.
                     * If another image is in the output queue it can be acquired now using GetCurrentImage(). */
                }
            }
            catch (Exception e)
            {
                ShowException(e, m_imageProvider.GetLastErrorMessage());
            }
        }