Exemplo n.º 1
0
        private static object CoerceRectInterior(DependencyObject d, object value)
        {
            PuncturedRect pr         = (PuncturedRect)d;
            Rect          rcExterior = pr.RectExterior;
            Rect          rcProposed = (Rect)value;
            double        left       = Math.Max(rcProposed.Left, rcExterior.Left);
            double        top        = Math.Max(rcProposed.Top, rcExterior.Top);
            double        width      = Math.Min(rcProposed.Right, rcExterior.Right) - left;
            double        height     = Math.Min(rcProposed.Bottom, rcExterior.Bottom) - top;

            if (width < 0 || height < 0)
            {
                return(pr.RectInterior);
            }

            rcProposed = new Rect(left, top, width, height);
            return(rcProposed);
        }
Exemplo n.º 2
0
        public CroppingAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            CropEnabled     = false;
            _visualChildren = new VisualCollection(this);

            _drawingSelection = false;
            _adornedElement   = adornedElement;
            _selectRect       = new Rect();
            _geometry         = new RectangleGeometry();

            var dashes = new DoubleCollection(new double[] { 6, 1 });

            Rubberband = new Path
            {
                Data            = _geometry,
                StrokeThickness = 2,
                Stroke          = Brushes.White,
                StrokeDashArray = dashes,
                Opacity         = 0.6,
                Visibility      = Visibility.Hidden
            };

            _visualChildren.Add(Rubberband);

            // A transparent black background to show outside the crop area over the rest
            // of the image
            Fill = new SolidColorBrush(Color.FromArgb(110, 0, 0, 0));

            _cropMask = new PuncturedRect();
            _cropMask.IsHitTestVisible = false;
            _cropMask.RectInterior     = _selectRect;
            _cropMask.Fill             = Fill;
            _cropMask.Visibility       = Visibility.Hidden;

            _visualChildren.Add(_cropMask);

            // We're going to put the thumbs in a canvas, since the events and placement
            // actually work correctly in a canvas.
            _thumbsCanvas = new Canvas();
            _thumbsCanvas.HorizontalAlignment = HorizontalAlignment.Stretch;
            _thumbsCanvas.VerticalAlignment   = VerticalAlignment.Stretch;
            _visualChildren.Add(_thumbsCanvas);

            // Call a helper method to initialize the Thumbs
            // with a customized cursors.

            _thumbSize = new Size(10, 10);
            BuildAdornerCorner(ref _topLeft, Cursors.SizeNWSE);
            BuildAdornerCorner(ref _topRight, Cursors.SizeNESW);
            BuildAdornerCorner(ref _bottomLeft, Cursors.SizeNESW);
            BuildAdornerCorner(ref _bottomRight, Cursors.SizeNWSE);

            // Add handlers for resizing.
            _bottomLeft.DragDelta  += HandleBottomLeft;
            _bottomRight.DragDelta += HandleBottomRight;
            _topLeft.DragDelta     += HandleTopLeft;
            _topRight.DragDelta    += HandleTopRight;

            MouseMove += DrawSelection;
            MouseUp   += EndSelection;

            SetElementVisibility(Visibility.Hidden);
        }