protected override void OnRender(DrawingContext drawingContext)
        {
            ScreenReplicator replicator = Visual as ScreenReplicator;

            if (replicator != null)
            {
                if (replicator.IsRunning)
                {
                    if (replicator.IsReplicating)
                    {
                        _screenBrush = CreateImageBrush(replicator);
                    }
                    else if (replicator.BlankOnStop)
                    {
                        _screenBrush = null;
                    }

                    if (_screenBrush != null)
                    {
                        drawingContext.DrawRectangle(_screenBrush, null, _displayRect);
                    }
                }
                else
                {
                    drawingContext.DrawRectangle(null, _inactivePen, _displayRect);
                    _inactiveTextFormat.RenderText(drawingContext, _inactiveBrush, "Screen Replicator", _displayRect);
                }
            }
        }
        protected override void OnRefresh()
        {
            ScreenReplicator replicator = Visual as ScreenReplicator;

            _inactiveBrush         = Brushes.Yellow;
            _inactivePen           = new Pen(_inactiveBrush, 1d);
            _inactivePen.DashStyle = DashStyles.Dash;
            _inactiveTextFormat    = new TextFormat();
            _inactiveTextFormat.VerticalAlignment = TextVerticalAlignment.Center;
            _inactiveTextFormat.VerticalAlignment = TextVerticalAlignment.Center;

            if (replicator != null)
            {
                _displayRect.Width  = replicator.Width;
                _displayRect.Height = replicator.Height;
            }
        }
        private ImageBrush CreateImageBrush(ScreenReplicator replicator)
        {
            ImageBrush brush = null;
            _hDC = NativeMethods.GetDC(NativeMethods.GetDesktopWindow());
            _hMemDC = NativeMethods.CreateCompatibleDC(_hDC);
            _hBitmap = NativeMethods.CreateCompatibleBitmap(_hDC, replicator.CaptureRectangle.Width, replicator.CaptureRectangle.Height);
            if (_hBitmap != IntPtr.Zero)
            {
                IntPtr hOld = (IntPtr)NativeMethods.SelectObject(_hMemDC, _hBitmap);

                NativeMethods.BitBlt(_hMemDC, 0, 0, replicator.CaptureRectangle.Width, replicator.CaptureRectangle.Height, _hDC,
                                           replicator.CaptureRectangle.X, replicator.CaptureRectangle.Y, NativeMethods.SRCCOPY);

                NativeMethods.SelectObject(_hMemDC, hOld);
                BitmapSource bmp = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(_hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(replicator.CaptureRectangle.Width, replicator.CaptureRectangle.Height));

                brush = new ImageBrush(bmp);
                NativeMethods.DeleteObject(_hBitmap);
            }
            NativeMethods.DeleteDC(_hMemDC);
            NativeMethods.ReleaseDC(NativeMethods.GetDesktopWindow(), _hDC);
            return brush;
        }
        private ImageBrush CreateImageBrush(ScreenReplicator replicator)
        {
            ImageBrush brush = null;

            _hDC     = NativeMethods.GetDC(NativeMethods.GetDesktopWindow());
            _hMemDC  = NativeMethods.CreateCompatibleDC(_hDC);
            _hBitmap = NativeMethods.CreateCompatibleBitmap(_hDC, replicator.CaptureRectangle.Width, replicator.CaptureRectangle.Height);
            if (_hBitmap != IntPtr.Zero)
            {
                IntPtr hOld = (IntPtr)NativeMethods.SelectObject(_hMemDC, _hBitmap);

                NativeMethods.BitBlt(_hMemDC, 0, 0, replicator.CaptureRectangle.Width, replicator.CaptureRectangle.Height, _hDC,
                                     replicator.CaptureRectangle.X, replicator.CaptureRectangle.Y, NativeMethods.SRCCOPY);

                NativeMethods.SelectObject(_hMemDC, hOld);
                BitmapSource bmp = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(_hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(replicator.CaptureRectangle.Width, replicator.CaptureRectangle.Height));

                brush = new ImageBrush(bmp);
                NativeMethods.DeleteObject(_hBitmap);
            }
            NativeMethods.DeleteDC(_hMemDC);
            NativeMethods.ReleaseDC(NativeMethods.GetDesktopWindow(), _hDC);
            return(brush);
        }