Beispiel #1
0
        /// <summary>
        /// Start sample image capture continuous.
        /// </summary>
        /// <param name="capture">The capture provider.</param>
        /// <param name="captureHandler">The capture method where image data is to be written to.</param>
        /// <param name="imageCodecInfo">The System.Drawing.Imaging.ImageCodecInfo class provides the necessary storage
        ///  members and methods to retrieve all pertinent information about the installed
        ///  image encoders and decoders (called codecs)</param>
        /// <param name="encoderParameters">Encapsulates an array of System.Drawing.Imaging.EncoderParameter objects.</param>
        private void StartSampleImageCaptureContinuous(CaptureImageSample capture, Action <MemoryStream> captureHandler, ImageCodecInfo imageCodecInfo, EncoderParameters encoderParameters)
        {
            MemoryStream stream = new MemoryStream(20000);
            Bitmap       image  = null;
            IntPtr       ip     = IntPtr.Zero;

            // While not shutting down, and still capturing.
            while (!_stop)
            {
                try
                {
                    // capture image
                    ip    = capture.GetSnapshotImagePointer();
                    image = new Bitmap(capture.Width, capture.Height, capture.Stride, PixelFormat.Format24bppRgb, ip);
                    image.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    image.Save(stream, imageCodecInfo, encoderParameters);

                    // Send the image to the client.
                    captureHandler(stream);

                    // Empty the stream
                    stream.SetLength(0);

                    // remove the image from memory
                    image.Dispose();
                    image = null;
                }
                catch (Exception) { }
                finally
                {
                    if (ip != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(ip);
                        ip = IntPtr.Zero;
                    }
                }
            }

            try { }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }

                if (image != null)
                {
                    image.Dispose();
                }
            }
        }