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 overlay to ImageCropper #364

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 8 additions & 4 deletions components/ImageCropper/samples/ImageCropper.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The `ImageCropper Control` allows user to freely crop an image.

> [!Sample ImageCropperSample]

## Syntax
### Syntax

```xaml
<Page ...
Expand All @@ -25,9 +25,7 @@ The `ImageCropper Control` allows user to freely crop an image.
</Page>
```

## Examples

### Use ImageCropper
### Basic usage

You can set the cropped image source by using the `LoadImageFromFile(StorageFile)` method or setting the `Source` property.

Expand Down Expand Up @@ -66,3 +64,9 @@ Or you can crop image without aspect ratio.
```csharp
ImageCropper.AspectRatio = null;
```

### With an Overlay

The crop area can be overlaid with a brush. Here we use a `RadialGradientBrush`, but you can use any brush; backdrop Media brushes, Geometry brushes, bitmap and image brushes, and so on.

> [!Sample ImageCropperOverlaySample]
25 changes: 25 additions & 0 deletions components/ImageCropper/samples/ImageCropperOverlaySample.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Page
x:Class="ImageCropperExperiment.Samples.ImageCropperOverlaySample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
xmlns:local="using:ImageCropperExperiment.Samples"
xmlns:media="using:Microsoft.UI.Xaml.Media">
<Grid>
<controls:ImageCropper
x:Name="ImageCropper"
Width="520"
Height="520"
AspectRatio="{x:Bind local:ImageCropperSample.ConvertStringToAspectRatio(AspectRatioSetting), Mode=OneWay}"
CropShape="{x:Bind local:ImageCropperSample.ConvertStringToCropShape(CropShapeSetting), Mode=OneWay}"
ThumbPlacement="{x:Bind local:ImageCropperSample.ConvertStringToThumbPlacement(ThumbPlacementSetting), Mode=OneWay}">
<controls:ImageCropper.Overlay>
<media:RadialGradientBrush>
<GradientStop Offset="0.75" Color="Transparent" />
<GradientStop Offset="1" Color="DimGray" />
</media:RadialGradientBrush>
</controls:ImageCropper.Overlay>
</controls:ImageCropper>

</Grid>
</Page>
28 changes: 28 additions & 0 deletions components/ImageCropper/samples/ImageCropperOverlaySample.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.Storage;

namespace ImageCropperExperiment.Samples;

[ToolkitSampleMultiChoiceOption("ThumbPlacementSetting", "All", "Corners", Title = "Thumb Placement")]
[ToolkitSampleMultiChoiceOption("CropShapeSetting", "Circular", "Rectangular", Title = "Crop Shape")]
[ToolkitSampleMultiChoiceOption("AspectRatioSetting", "Custom", "Square", "Landscape(16:9)", "Portrait(9:16)", "4:3", "3:2", Title = "Aspect Ratio")]

[ToolkitSample(id: nameof(ImageCropperOverlaySample), "ImageCropper Overlay", description: $"A sample for showing how to use the overlay feature of {nameof(ImageCropper)}.")]
public sealed partial class ImageCropperOverlaySample : Page
{
public ImageCropperOverlaySample()
{
this.InitializeComponent();

_ = Load();
}

private async Task Load()
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Owl.jpg"));
await ImageCropper.LoadImageFromFile(file);
}
}
5 changes: 5 additions & 0 deletions components/ImageCropper/src/ImageCropper.Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public partial class ImageCropper
/// </summary>
private const string MaskAreaPathPartName = "PART_MaskAreaPath";

/// <summary>
/// Key of the overlay layer.
/// </summary>
private const string OverlayAreaPathPartName = "PART_OverlayAreaPath";

/// <summary>
/// Key of the ImageCropperThumb that on the top.
/// </summary>
Expand Down
26 changes: 24 additions & 2 deletions components/ImageCropper/src/ImageCropper.Logic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,20 @@ private void UpdateCropShape()
{
case CropShape.Rectangular:
_innerGeometry = new RectangleGeometry();
_overlayGeometry = new RectangleGeometry();
break;
case CropShape.Circular:
_innerGeometry = new EllipseGeometry();
_overlayGeometry = new EllipseGeometry();
break;
}

_maskAreaGeometryGroup.Children.Add(_outerGeometry);
_maskAreaGeometryGroup.Children.Add(_innerGeometry);
if (_overlayAreaPath != null)
{
_overlayAreaPath.Data = _overlayGeometry;
}
}

/// <summary>
Expand All @@ -473,7 +479,7 @@ private void UpdateMaskArea(bool animate = false)
switch (CropShape)
{
case CropShape.Rectangular:
if (_innerGeometry is RectangleGeometry rectangleGeometry)
var updateRectangleGeometry = (RectangleGeometry rectangleGeometry) =>
{
var to = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
if (animate)
Expand All @@ -486,11 +492,19 @@ private void UpdateMaskArea(bool animate = false)
{
rectangleGeometry.Rect = to;
}
};
if (_innerGeometry is RectangleGeometry innerRectangleGeometry)
{
updateRectangleGeometry(innerRectangleGeometry);
}
if (_overlayGeometry is RectangleGeometry overlayRectangleGeometry)
{
updateRectangleGeometry(overlayRectangleGeometry);
}

break;
case CropShape.Circular:
if (_innerGeometry is EllipseGeometry ellipseGeometry)
var updateEllipseGeometry = (EllipseGeometry ellipseGeometry) =>
{
var center = new Point(((_endX - _startX) / 2) + _startX, ((_endY - _startY) / 2) + _startY);
var radiusX = (_endX - _startX) / 2;
Expand All @@ -509,6 +523,14 @@ private void UpdateMaskArea(bool animate = false)
ellipseGeometry.RadiusX = radiusX;
ellipseGeometry.RadiusY = radiusY;
}
};
if (_innerGeometry is EllipseGeometry innerEllipseGeometry)
{
updateEllipseGeometry(innerEllipseGeometry);
}
if (_overlayGeometry is EllipseGeometry overlayEllipseGeometry)
{
updateEllipseGeometry(overlayEllipseGeometry);
}

break;
Expand Down
15 changes: 15 additions & 0 deletions components/ImageCropper/src/ImageCropper.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public Brush Mask
set { SetValue(MaskProperty, value); }
}

/// <summary>
/// Gets or sets the overlay on the cropped image.
/// </summary>
public Brush Overlay
{
get { return (Brush)GetValue(OverlayProperty); }
set { SetValue(OverlayProperty, value); }
}

/// <summary>
/// Gets or sets a value for the style to use for the primary thumbs of the ImageCropper.
/// </summary>
Expand Down Expand Up @@ -175,6 +184,12 @@ public ThumbPlacement ThumbPlacement
public static readonly DependencyProperty MaskProperty =
DependencyProperty.Register(nameof(Mask), typeof(Brush), typeof(ImageCropper), new PropertyMetadata(default(Brush)));

/// <summary>
/// Identifies the <see cref="Overlay"/> dependency property.
/// </summary>
public static readonly DependencyProperty OverlayProperty =
DependencyProperty.Register(nameof(Overlay), typeof(Brush), typeof(ImageCropper), new PropertyMetadata(default(Brush)));

/// <summary>
/// Identifies the <see cref="PrimaryThumbStyle"/> dependency property.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions components/ImageCropper/src/ImageCropper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace CommunityToolkit.WinUI.Controls;
[TemplatePart(Name = ImageCanvasPartName, Type = typeof(Canvas))]
[TemplatePart(Name = SourceImagePartName, Type = typeof(Image))]
[TemplatePart(Name = MaskAreaPathPartName, Type = typeof(Path))]
[TemplatePart(Name = OverlayAreaPathPartName, Type = typeof(Path))]
[TemplatePart(Name = TopThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = BottomThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = LeftThumbPartName, Type = typeof(ImageCropperThumb))]
Expand All @@ -39,6 +40,7 @@ public partial class ImageCropper : Control
private Canvas? _imageCanvas;
private Image? _sourceImage;
private Path? _maskAreaPath;
private Path? _overlayAreaPath;
private ImageCropperThumb? _topThumb;
private ImageCropperThumb? _bottomThumb;
private ImageCropperThumb? _leftThumb;
Expand All @@ -59,6 +61,7 @@ public partial class ImageCropper : Control
private Rect _restrictedSelectRect = Rect.Empty;
private RectangleGeometry _outerGeometry;
private Geometry _innerGeometry;
private Geometry _overlayGeometry;
private TimeSpan _animationDuration = TimeSpan.FromSeconds(0.3);

/// <summary>
Expand Down Expand Up @@ -165,6 +168,7 @@ protected override void OnApplyTemplate()
_imageCanvas = GetTemplateChild(ImageCanvasPartName) as Canvas;
_sourceImage = GetTemplateChild(SourceImagePartName) as Image;
_maskAreaPath = GetTemplateChild(MaskAreaPathPartName) as Path;
_overlayAreaPath = GetTemplateChild(OverlayAreaPathPartName) as Path;
_topThumb = GetTemplateChild(TopThumbPartName) as ImageCropperThumb;
_bottomThumb = GetTemplateChild(BottomThumbPartName) as ImageCropperThumb;
_leftThumb = GetTemplateChild(LeftThumbPartName) as ImageCropperThumb;
Expand Down Expand Up @@ -195,6 +199,11 @@ private void HookUpEvents()
_maskAreaPath.Data = _maskAreaGeometryGroup;
}

if (_overlayAreaPath != null)
{
_overlayAreaPath.Data = _overlayGeometry;
}

if (_topThumb != null)
{
_topThumb.Position = ThumbPosition.Top;
Expand Down
3 changes: 3 additions & 0 deletions components/ImageCropper/src/ImageCropper.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
Source="{TemplateBinding Source}" />
<Path x:Name="PART_MaskAreaPath"
Fill="{TemplateBinding Mask}" />
<Path x:Name="PART_OverlayAreaPath"
Fill="{TemplateBinding Overlay}"
IsHitTestVisible="False" />
<controls:ImageCropperThumb x:Name="PART_TopThumb"
Style="{TemplateBinding SecondaryThumbStyle}" />
<!-- ui:FrameworkElementExtensions.Cursor="SizeNorthSouth" -->
Expand Down
Loading