Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdornerLayer goes outside Border if I zoom the picture WPF

I created the logic that crops an image that is contained inside a border that is inside a grid. The grid has many borders, so this grid will have many pictures. The problem is that when I zoom the picture the logic zoomed the picture (which is okay) but when I use the crop logic the AdornerLayer goes outside the border like the picture: Image zoomed with crop selector

On this image the pic doesn't have zoom, so the AdornerLayer is correct:enter image description here

The code that I'm using to add the crop to the image:

private void AddCropToElement(FrameworkElement fel, System.Drawing.Image img)
{
    if (!cropElements.ContainsKey(Convert.ToString(((Image)fel).Source)))
    {
        if (_felCur != null)
        {
            RemoveCropFromCur();
        }

        rcInterior = new Rect(
            fel.ActualWidth * 0.2,
            fel.ActualHeight * 0.2,
            fel.ActualWidth * 0.6,
            fel.ActualHeight * 0.6);
        rectMoving = false;
        Rect newRect = scaleRect(rcInterior, img);
        imgCropMove = img;

        AdornerLayer aly = AdornerLayer.GetAdornerLayer(fel);
        _clp = new CroppingAdorner(fel, rcInterior);
        aly.Add(_clp);
        cropElements.Add(Convert.ToString(((Image)fel).Source), fel);

        imageCropped = _clp.Crop(new System.Drawing.Bitmap(img), newRect);


        _clp.CropChanged += HandleCropChanged;
        _felCur = fel;
    }
}

In this case the object named fel is the picture that I want to crop and the Border is his parent.

How I can fix the problem of the AdornerLayout that goes outside if the image is zoomed?

like image 948
avmauricio Avatar asked Oct 08 '13 19:10

avmauricio


1 Answers

Are you using the default Window Adorner or have you created a custom AdornerDecorator around your Border in your XAML?

<AdornerDecorator>
    <Border>...</Border>
</AdornerDecorator>

Additionally, if you are applying a zoom factor on your Border, you can add a Binding on your cropping display rectangle to match the Scale on your Border object.

like image 194
CMerat Avatar answered Oct 31 '22 02:10

CMerat