Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CroppedRegionChanged event for ImageCropper. #3179

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void ImageCropperThumb_KeyUp(object sender, KeyRoutedEventArgs e)
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
{
croppedRect.Intersect(_restrictedCropRect);
_currentCroppedRect = croppedRect;
CroppedRegion = croppedRect;
}

UpdateImageLayout(true);
Expand All @@ -116,7 +116,7 @@ private void ImageCropperThumb_ManipulationCompleted(object sender, Manipulation
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
{
croppedRect.Intersect(_restrictedCropRect);
_currentCroppedRect = croppedRect;
CroppedRegion = croppedRect;
}

UpdateImageLayout(true);
Expand Down Expand Up @@ -160,7 +160,7 @@ private void SourceImage_ManipulationDelta(object sender, ManipulationDeltaRoute
selectedRect.Y += offsetY;
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
croppedRect.Intersect(_restrictedCropRect);
_currentCroppedRect = croppedRect;
CroppedRegion = croppedRect;
UpdateImageLayout();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void InitImageLayout(bool animate = false)
_restrictedCropRect = new Rect(0, 0, Source.PixelWidth, Source.PixelHeight);
if (IsValidRect(_restrictedCropRect))
{
_currentCroppedRect = KeepAspectRatio ? GetUniformRect(_restrictedCropRect, UsedAspectRatio) : _restrictedCropRect;
CroppedRegion = KeepAspectRatio ? GetUniformRect(_restrictedCropRect, UsedAspectRatio) : _restrictedCropRect;
UpdateImageLayout(animate);
}
}
Expand All @@ -44,8 +44,8 @@ private void UpdateImageLayout(bool animate = false)
{
if (Source != null && IsValidRect(CanvasRect))
{
var uniformSelectedRect = GetUniformRect(CanvasRect, _currentCroppedRect.Width / _currentCroppedRect.Height);
UpdateImageLayoutWithViewport(uniformSelectedRect, _currentCroppedRect, animate);
var uniformSelectedRect = GetUniformRect(CanvasRect, CroppedRegion.Width / CroppedRegion.Height);
UpdateImageLayoutWithViewport(uniformSelectedRect, CroppedRegion, animate);
}
}

Expand All @@ -69,7 +69,7 @@ private void UpdateImageLayoutWithViewport(Rect viewport, Rect viewportImageRect
_inverseImageTransform.ScaleX = _inverseImageTransform.ScaleY = 1 / imageScale;
_inverseImageTransform.TranslateX = -_imageTransform.TranslateX / imageScale;
_inverseImageTransform.TranslateY = -_imageTransform.TranslateY / imageScale;
var selectedRect = _imageTransform.TransformBounds(_currentCroppedRect);
var selectedRect = _imageTransform.TransformBounds(CroppedRegion);
_restrictedSelectRect = _imageTransform.TransformBounds(_restrictedCropRect);
var startPoint = GetSafePoint(_restrictedSelectRect, new Point(selectedRect.X, selectedRect.Y));
var endPoint = GetSafePoint(_restrictedSelectRect, new Point(
Expand Down Expand Up @@ -271,7 +271,7 @@ private void UpdateCroppedRect(ThumbPosition position, Point diffPos)
var croppedRect = _inverseImageTransform.TransformBounds(
new Rect(startPoint, endPoint));
croppedRect.Intersect(_restrictedCropRect);
_currentCroppedRect = croppedRect;
CroppedRegion = croppedRect;
var viewportRect = GetUniformRect(CanvasRect, selectedRect.Width / selectedRect.Height);
var viewportImgRect = _inverseImageTransform.TransformBounds(selectedRect);
UpdateImageLayoutWithViewport(viewportRect, viewportImgRect);
Expand Down Expand Up @@ -557,7 +557,7 @@ private void UpdateAspectRatio(bool animate = false)

var croppedRect = _inverseImageTransform.TransformBounds(uniformSelectedRect);
croppedRect.Intersect(_restrictedCropRect);
_currentCroppedRect = croppedRect;
CroppedRegion = croppedRect;
UpdateImageLayout(animate);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public partial class ImageCropper
/// </summary>
public double MinSelectedLength { get; set; } = 40;

/// <summary>
/// Gets the current cropped region.
/// </summary>
public Rect CroppedRegion => _currentCroppedRect;

private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (ImageCropper)d;
Expand Down Expand Up @@ -64,6 +59,13 @@ private static void OnCropShapeChanged(
target.UpdateMaskArea();
}

private static void OnCroppedRegionChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (ImageCropper)d;
target.CroppedRegionChanged?.Invoke(target, new ImageCropperCroppedRegionChangedEventArgs { OldRegion = (Rect)e.OldValue, NewRegion = (Rect)e.NewValue });
}

private static void OnThumbPlacementChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Expand Down Expand Up @@ -99,6 +101,15 @@ public CropShape CropShape
set { SetValue(CropShapeProperty, value); }
}

/// <summary>
/// Gets the current cropped region.
/// </summary>
public Rect CroppedRegion
{
get { return (Rect)GetValue(CroppedRegionProperty); }
private set { SetValue(CroppedRegionProperty, value); }
}

/// <summary>
/// Gets or sets the mask on the cropped image.
/// </summary>
Expand Down Expand Up @@ -153,6 +164,12 @@ public ThumbPlacement ThumbPlacement
public static readonly DependencyProperty CropShapeProperty =
DependencyProperty.Register(nameof(CropShape), typeof(CropShape), typeof(ImageCropper), new PropertyMetadata(default(CropShape), OnCropShapeChanged));

/// <summary>
/// Identifies the <see cref="CroppedRegion"/> dependency property.
/// </summary>
public static readonly DependencyProperty CroppedRegionProperty =
DependencyProperty.Register(nameof(CroppedRegion), typeof(Rect), typeof(ImageCropper), new PropertyMetadata(Rect.Empty, OnCroppedRegionChanged));

/// <summary>
/// Identifies the <see cref="Mask"/> dependency property.
/// </summary>
Expand Down
13 changes: 8 additions & 5 deletions Microsoft.Toolkit.Uwp.UI.Controls/ImageCropper/ImageCropper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
Expand Down Expand Up @@ -54,7 +53,6 @@ public partial class ImageCropper : Control
private double _startY;
private double _endX;
private double _endY;
private Rect _currentCroppedRect = Rect.Empty;
private Rect _restrictedCropRect = Rect.Empty;
private Rect _restrictedSelectRect = Rect.Empty;
private RectangleGeometry _outerGeometry;
Expand All @@ -69,6 +67,11 @@ public ImageCropper()
DefaultStyleKey = typeof(ImageCropper);
}

/// <summary>
/// Occurs when the <see cref="CroppedRegion"/> property has changed.
/// </summary>
public event TypedEventHandler<ImageCropper, ImageCropperCroppedRegionChangedEventArgs> CroppedRegionChanged;

private Rect CanvasRect => new Rect(0, 0, _imageCanvas?.ActualWidth ?? 0, _imageCanvas?.ActualHeight ?? 0);

private bool KeepAspectRatio => UsedAspectRatio > 0;
Expand Down Expand Up @@ -398,11 +401,11 @@ public async Task SaveAsync(IRandomAccessStream stream, BitmapFileFormat bitmapF

if (keepRectangularOutput || CropShape == CropShape.Rectangular)
{
await CropImageAsync(Source, stream, _currentCroppedRect, bitmapFileFormat);
await CropImageAsync(Source, stream, CroppedRegion, bitmapFileFormat);
return;
}

await CropImageWithShapeAsync(Source, stream, _currentCroppedRect, bitmapFileFormat, CropShape);
await CropImageWithShapeAsync(Source, stream, CroppedRegion, bitmapFileFormat, CropShape);
}

/// <summary>
Expand Down Expand Up @@ -439,7 +442,7 @@ public bool TrySetCroppedRegion(Rect rect)
return false;
}

_currentCroppedRect = rect;
CroppedRegion = rect;
UpdateImageLayout(true);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.Foundation;

namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
///为< 请参阅cref = “ ImageCropper.CroppedRegionChanged ” />事件提供事件数据。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like something happened with GitHub accepting the comment? This is causing the CI to fail as it's not a valid XML doc comment anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that github accepted the changes that were automatically translated by Google Translate (because my chrome automatically translated the webpage into Chinese), maybe it should be a github bug? 🤣
Let me fix it.

/// </summary>
public sealed class ImageCropperCroppedRegionChangedEventArgs
{
/// <summary>
/// Gets the region that is currently cropped in the control.
/// </summary>
public Rect NewRegion { get; internal set; }

/// <summary>
/// Gets the region that was previously cropped in the control.
/// </summary>
public Rect OldRegion { get; internal set; }
}
}