private void NewFrameDelegateMethod(SampleGrabberBufferCBEventArgs e)
        {
            if (_stopFlag)
            {
                return;
            }

            try
            {
                if (pnScreen == null)
                {
                    return;
                }

                if (_frameStopped)
                {
                    return;
                }

                if (_frameBitmap == null || _frameBitmap.PixelWidth != e.Width || _frameBitmap.PixelHeight != e.Height)
                {
                    _frameBitmap = new WriteableBitmap(e.Width, e.Height, 72, 72, PixelFormats.Bgr24, null);

                    pnScreen.BeginInit();
                    pnScreen.Source = _frameBitmap;
                    pnScreen.EndInit();
                }

                pnScreen.BeginInit();

                if (pnScreen.Source == null)
                {
                    pnScreen.Source = _frameBitmap;
                }

                int lineStep = (((e.Width * 24) + 31) / 32) * 4;
                _frameBitmap.WritePixels(new Int32Rect(0, 0, e.Width, e.Height), e.Buffer, (int)e.BufferLen, lineStep);
                pnScreen.EndInit();
            }
            catch
            {
            }
        }
        private void VideoCapture1_OnVideoFrameBuffer(object sender, SampleGrabberBufferCBEventArgs e)
        {
            try
            {
                if (_stopFlag)
                {
                    return;
                }

                Dispatcher.BeginInvoke(new NewFrameDelegate(NewFrameDelegateMethod), e);

                if (_tempBuffer == IntPtr.Zero)
                {
                    _tempBuffer = Marshal.AllocCoTaskMem(ImageHelper.GetStrideRGB24(e.Width) * e.Height);
                }

                // live
                if (_searchLiveData == null)
                {
                    _searchLiveData = new FingerprintLiveData(TimeSpan.FromMilliseconds(_fragmentDuration), DateTime.Now);
                    _fragmentCount++;
                }

                long timestamp = (long)(e.SampleTime * 1000);
                if (timestamp < _fragmentDuration * _fragmentCount)
                {
                    ImageHelper.CopyMemory(_tempBuffer, e.Buffer, e.BufferLen);

                    // process frame to remove ignored areas
                    if (_ignoredAreas.Count > 0)
                    {
                        foreach (var area in _ignoredAreas)
                        {
                            if (area.Right > e.Width || area.Bottom > e.Height)
                            {
                                continue;
                            }

                            MFP.FillColor(_tempBuffer, e.Width, e.Height, area, 0);
                        }
                    }

                    VFPSearch.Process(_tempBuffer, e.Width, e.Height, ImageHelper.GetStrideRGB24(e.Width), TimeSpan.FromMilliseconds(timestamp), ref _searchLiveData.Data);
                }
                else
                {
                    _fingerprintQueue.Enqueue(_searchLiveData);

                    _searchLiveData = null;

                    Dispatcher.BeginInvoke(new ProcessVideoDelegate(ProcessVideoDelegateMethod));
                }

                // overlap
                if (timestamp < _fragmentDuration / 2)
                {
                    return;
                }

                if (_searchLiveOverlapData == null)
                {
                    _searchLiveOverlapData = new FingerprintLiveData(TimeSpan.FromSeconds(_fragmentDuration), DateTime.Now);
                    _overlapFragmentCount++;
                }

                if (timestamp < _fragmentDuration * _overlapFragmentCount + _fragmentDuration / 2)
                {
                    ImageHelper.CopyMemory(_tempBuffer, e.Buffer, e.BufferLen);
                    VFPSearch.Process(_tempBuffer, e.Width, e.Height, ImageHelper.GetStrideRGB24(e.Width), TimeSpan.FromMilliseconds(timestamp), ref _searchLiveOverlapData.Data);
                }
                else
                {
                    _fingerprintQueue.Enqueue(_searchLiveOverlapData);

                    _searchLiveOverlapData = null;

                    Dispatcher.BeginInvoke(new ProcessVideoDelegate(ProcessVideoDelegateMethod));
                }
            }
            catch
            {
            }
        }