/// <summary> /// Create a new image capture object. /// </summary> /// <param name="capture">The capture provider.</param> public ImageCapture(CaptureImageSample capture) { if (capture == null) { throw new ArgumentException("The capture parameter must be set to a valid Filter.\n"); } _capture = capture; OnCreated(); }
/// <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(); } } }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> protected virtual void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { // If disposing equals true, dispose all managed // and unmanaged resources. if (disposing) { if (_capture != null) { _capture.Dispose(); } } // Call the appropriate methods to clean up // unmanaged resources here. _capture = null; // Note disposing has been done. disposed = true; } }