public void RefreshMostRecentFrame()
        {
            if (mostRecentFrame == null)
            {
                return;
            }

            //Prepare the temp frame for annotations
            if (tempFrame == null || !tempFrame.SameSizeAs(mostRecentFrame))
            {
                tempFrame = mostRecentFrame.Clone();
            }
            else
            {
                tempFrame.DrawFrame(mostRecentFrame);
            }

            tempFrame.FrameIndex      = mostRecentFrame.FrameIndex;
            tempFrame.ProcessorResult = mostRecentFrame.ProcessorResult;

            if (OnRefreshMostRecentFrame != null)
            {
                OnRefreshMostRecentFrame(tempFrame);
            }

            tempFrame.CopyToWriteableBitmap(canvasBuffer);
        }
        private void ShowFrame(Filters.Frame frame)
        {
            if (isDrawing)
            {
                return;
            }

            if (Application.Current == null)
            {
                return;
            }

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                if (Application.Current == null)
                {
                    return;
                }

                isDrawing = true;

                //Create WriteableBitmap the first time
                if (canvasBuffer == null || canvasBuffer.Height != frame.Height || canvasBuffer.Width != frame.Width)
                {
                    canvasBuffer  = new WriteableBitmap(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr24, null);
                    Canvas.Source = canvasBuffer;

                    mostRecentFrame = frame.Clone();
                }

                //Limit frame rate shown
                if ((DateTime.Now - prevFrameTime).TotalMilliseconds >= 1000.0 / maxFps)
                {
                    //Save the most recent frame
                    mostRecentFrame.DrawFrame(frame);
                    mostRecentFrame.ProcessorResult = frame.ProcessorResult;
                    mostRecentFrame.FrameIndex      = frame.FrameIndex;

                    if (OnShowFrame != null)
                    {
                        OnShowFrame(frame);
                    }

                    RefreshMostRecentFrame();

                    prevFrameTime = DateTime.Now;
                }

                _sliderValueChangedByCode = true;
                sliderTime.Value          = frame.FramePercentage * 1000;
                lblTime.Content           = frame.FrameTime.ToString();

                //Compute FPS
                var now = DateTime.Now;
                fpsHist.AddLast(now);
                lblFPS.Content = string.Format("FPS: {0:n1}", fpsHist.Count / 1.0);

                while ((now - fpsHist.First()).TotalMilliseconds > 1000)
                {
                    fpsHist.RemoveFirst();
                }

                isDrawing = false;
            }));
        }