public CroppingAdorner(UIElement adornedElement) : base(adornedElement)
        {
            _visualCollection   = new VisualCollection(this);
            _originalCanvas     = (Canvas)adornedElement;
            _canvasOverlay      = new Canvas();
            _rectangleManager   = new RectangleManager(_canvasOverlay);
            _overlayManager     = new OverlayManager(_canvasOverlay, _rectangleManager);
            _thumbManager       = new ThumbManager(_canvasOverlay, _rectangleManager);
            _displayTextManager = new DisplayTextManager(_canvasOverlay, _rectangleManager);
            _visualCollection.Add(_canvasOverlay);

            //add event handlers
            MouseLeftButtonDown += MouseLeftButtonDownEventHandler;
            MouseMove           += MouseMoveEventHandler;
            MouseLeftButtonUp   += MouseLeftButtonUpEventHandler;
            Loaded += (object sender, RoutedEventArgs args) => Show();
            _originalCanvas.SizeChanged                   += (object sender, SizeChangedEventArgs e) => Show();
            _rectangleManager.RectangleSizeChanged        += (object sender, EventArgs args) => Show();
            _rectangleManager.OnRectangleDoubleClickEvent += (object sender, EventArgs args) =>
            {
                OnRectangleDoubleClickEvent?.Invoke(sender, new DoubleClickEventArgs()
                {
                    BitmapFrame = GetCroppedBitmapFrame()
                });
            };
        }
Exemplo n.º 2
0
        public CroppingAdorner(UIElement adornedElement) : base(adornedElement)
        {
            _visualCollection   = new VisualCollection(this);
            _canvasOverlay      = new Canvas();
            _rectangleManager   = new RectangleManager(_canvasOverlay);
            _overlayManager     = new OverlayManager(_canvasOverlay, _rectangleManager);
            _thumbManager       = new ThumbManager(_canvasOverlay, _rectangleManager);
            _displayTextManager = new DisplayTextManager(_canvasOverlay, _rectangleManager);
            _visualCollection.Add(_canvasOverlay);

            MouseLeftButtonDown += MouseLeftButtonDownEventHandler;
            MouseMove           += MouseMoveEventHandler;
            MouseLeftButtonUp   += MouseLeftButtonUpEventHandler;

            //add overlay
            Loaded += (sender, args) => { _overlayManager.UpdateOverlay(); };

            //if rectangle size changed, re-draw overlay
            _rectangleManager.RectangleSizeChanged += (sender, args) =>
            {
                _overlayManager.UpdateOverlay();
                _displayTextManager.UpdateSizeText();
            };
            _rectangleManager.OnRectangleDoubleClickEvent += (sender, args) => OnRectangleDoubleClickEvent?.Invoke(sender, new DoubleClickEventArgs {
                BitmapFrame = GetCroppedBitmapFrame()
            });
        }
        /// <summary>
        ///     Event handler for mouse left button
        /// </summary>
        /// <param name="e">Mouse event args</param>
        public void MouseLeftButtonDownEventHandler(MouseButtonEventArgs e)
        {
            _canvas.CaptureMouse();

            //get mouse click point relative to canvas overlay
            Point mouseClickPoint = e.GetPosition(_canvas);

            //first, we need to know where we click
            TouchPoint touch = GetTouchPoint(mouseClickPoint);

            //if we click outside of rectangle and rectangle already exist,
            //start recreating
            if ((Math.Abs(_rectangle.Height) > Tolerance ||
                 Math.Abs(_rectangle.Width) > Tolerance) &&
                touch == TouchPoint.OutsideRectangle)
            {
                //reset existing rectangle
                UpdateRectangle(0,
                                0,
                                0,
                                0);

                //start drawing
                _isDrawing = true;
            }

            //if rectangle not created - start creating
            if (Math.Abs(_rectangle.Height) < Tolerance &&
                Math.Abs(_rectangle.Width) < Tolerance)
            {
                _mouseStartPoint = mouseClickPoint;
                _isDrawing       = true;
            }

            //if rectangle is created and we click inside rectangle - start dragging
            if (Math.Abs(_rectangle.Height) > Tolerance &&
                Math.Abs(_rectangle.Width) > Tolerance &&
                touch != TouchPoint.OutsideRectangle)
            {
                if (e.ClickCount == 2)
                {
                    OnRectangleDoubleClickEvent?.Invoke(this,
                                                        EventArgs.Empty);
                    return;
                }

                _isDragging     = true;
                _mouseLastPoint = mouseClickPoint;
            }
        }