internal override Bitmap TakePicture(SynchronizedPictureBox pictureControl) { if (m_callbackCompleted != null) { return(null); } m_pictureControl = pictureControl; m_takePictureEnd = false; DsDevice cameraDevice = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice)[m_cameraDeviceIndex]; IFilterGraph2 filterGraph = null; IBaseFilter cam = null; IPin camCapture = null; // cam ISampleGrabber sg = null; IPin sgIn = null; // samplegrabber try { // setup filterGraph & connect camera filterGraph = (IFilterGraph2) new FilterGraph(); DsError.ThrowExceptionForHR(filterGraph.AddSourceFilterForMoniker(cameraDevice.Mon, null, cameraDevice.Name, out cam)); // setup smarttee and connect so that cam(PinCategory.Capture)->st(PinDirection.Input) camCapture = DsFindPin.ByCategory(cam, PinCategory.Capture, 0); // output ConfStreamDimensions((IAMStreamConfig)camCapture); // connect Camera output to SampleGrabber input sg = (ISampleGrabber) new SampleGrabber(); // configure AMMediaType media = new AMMediaType(); try { media.majorType = MediaType.Video; media.subType = BPP2MediaSubtype(m_configuration.BPP); // this will ask samplegrabber to do convertions for us media.formatType = FormatType.VideoInfo; DsError.ThrowExceptionForHR(sg.SetMediaType(media)); } finally { DsUtils.FreeAMMediaType(media); media = null; } DsError.ThrowExceptionForHR(sg.SetCallback(this, 1)); // 1 = BufferCB DsError.ThrowExceptionForHR(filterGraph.AddFilter((IBaseFilter)sg, "SG")); sgIn = DsFindPin.ByDirection((IBaseFilter)sg, PinDirection.Input, 0); // input DsError.ThrowExceptionForHR(filterGraph.Connect(camCapture, sgIn)); GetSizeInfo(sg); // wait until timeout - or picture has been taken if (m_callbackCompleted == null) { m_callbackCompleted = new ManualResetEvent(false); // start filter DsError.ThrowExceptionForHR(((IMediaControl)filterGraph).Run()); m_callbackState = 5; if (m_pictureControl != null) { m_callbackCompleted.WaitOne(); } else { if (!m_callbackCompleted.WaitOne(15000, false)) { throw new Exception(); //"Timeout while waiting for Picture"); } } return(m_capturedBitmap); } else { return(null); } } finally { // release allocated objects if (m_callbackCompleted != null) { m_callbackCompleted.Close(); m_callbackCompleted = null; } if (sgIn != null) { Marshal.ReleaseComObject(sgIn); sgIn = null; } if (sg != null) { Marshal.ReleaseComObject(sg); sg = null; } if (camCapture != null) { Marshal.ReleaseComObject(camCapture); camCapture = null; } if (cam != null) { Marshal.ReleaseComObject(cam); cam = null; } if (filterGraph != null) { try { ((IMediaControl)filterGraph).Stop(); } catch (Exception) { } Marshal.ReleaseComObject(filterGraph); filterGraph = null; } m_capturedBitmap = null; m_callbackCompleted = null; } }
internal abstract Bitmap TakePicture(SynchronizedPictureBox pictureControl);