forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCropper.Properties.cs
197 lines (172 loc) · 7.77 KB
/
ImageCropper.Properties.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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 System;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// The <see cref="ImageCropper"/> control allows user to crop image freely.
/// </summary>
public partial class ImageCropper
{
/// <summary>
/// Gets or sets the minimum cropped length(in pixel).
/// </summary>
public double MinCroppedPixelLength { get; set; } = 40;
/// <summary>
/// Gets or sets the minimum selectable length.
/// </summary>
public double MinSelectedLength { get; set; } = 40;
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (ImageCropper)d;
if (e.NewValue is WriteableBitmap bitmap)
{
if (bitmap.PixelWidth < target.MinCropSize.Width || bitmap.PixelHeight < target.MinCropSize.Height)
{
target.Source = null;
throw new ArgumentException("The resolution of the image is too small!");
}
}
target.InvalidateMeasure();
target.UpdateCropShape();
target.InitImageLayout();
}
private static void OnAspectRatioChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (ImageCropper)d;
target.UpdateAspectRatio(true);
}
private static void OnCropShapeChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (ImageCropper)d;
target.UpdateCropShape();
target.UpdateThumbsVisibility();
target.UpdateAspectRatio();
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)
{
var target = (ImageCropper)d;
target.UpdateThumbsVisibility();
}
/// <summary>
/// Gets or sets the source of the cropped image.
/// </summary>
public WriteableBitmap Source
{
get { return (WriteableBitmap)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
/// <summary>
/// Gets or sets the aspect ratio of the cropped image, the default value is null.
/// Only works when <see cref="CropShape"/> = <see cref="CropShape.Rectangular"/>.
/// </summary>
public double? AspectRatio
{
get { return (double?)GetValue(AspectRatioProperty); }
set { SetValue(AspectRatioProperty, value); }
}
/// <summary>
/// Gets or sets the shape to use when cropping.
/// </summary>
public CropShape CropShape
{
get { return (CropShape)GetValue(CropShapeProperty); }
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>
public Brush Mask
{
get { return (Brush)GetValue(MaskProperty); }
set { SetValue(MaskProperty, value); }
}
/// <summary>
/// Gets or sets a value for the style to use for the primary thumbs of the ImageCropper.
/// </summary>
public Style PrimaryThumbStyle
{
get { return (Style)GetValue(PrimaryThumbStyleProperty); }
set { SetValue(PrimaryThumbStyleProperty, value); }
}
/// <summary>
/// Gets or sets a value for the style to use for the secondary thumbs of the ImageCropper.
/// </summary>
public Style SecondaryThumbStyle
{
get { return (Style)GetValue(SecondaryThumbStyleProperty); }
set { SetValue(SecondaryThumbStyleProperty, value); }
}
/// <summary>
/// Gets or sets a value for thumb placement.
/// </summary>
public ThumbPlacement ThumbPlacement
{
get { return (ThumbPlacement)GetValue(ThumbPlacementProperty); }
set { SetValue(ThumbPlacementProperty, value); }
}
/// <summary>
/// Identifies the <see cref="AspectRatio"/> dependency property.
/// </summary>
public static readonly DependencyProperty AspectRatioProperty =
DependencyProperty.Register(nameof(AspectRatio), typeof(double?), typeof(ImageCropper), new PropertyMetadata(null, OnAspectRatioChanged));
/// <summary>
/// Identifies the <see cref="Source"/> dependency property.
/// </summary>
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register(nameof(Source), typeof(WriteableBitmap), typeof(ImageCropper), new PropertyMetadata(null, OnSourceChanged));
/// <summary>
/// Identifies the <see cref="CropShape"/> dependency property.
/// </summary>
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>
public static readonly DependencyProperty MaskProperty =
DependencyProperty.Register(nameof(Mask), typeof(Brush), typeof(ImageCropper), new PropertyMetadata(default(Brush)));
/// <summary>
/// Identifies the <see cref="PrimaryThumbStyle"/> dependency property.
/// </summary>
public static readonly DependencyProperty PrimaryThumbStyleProperty =
DependencyProperty.Register(nameof(PrimaryThumbStyle), typeof(Style), typeof(ImageCropper), new PropertyMetadata(default(Style)));
/// <summary>
/// Identifies the <see cref="SecondaryThumbStyle"/> dependency property.
/// </summary>
public static readonly DependencyProperty SecondaryThumbStyleProperty =
DependencyProperty.Register(nameof(SecondaryThumbStyle), typeof(Style), typeof(ImageCropper), new PropertyMetadata(default(Style)));
/// <summary>
/// Identifies the <see cref="ThumbPlacement"/> dependency property.
/// </summary>
public static readonly DependencyProperty ThumbPlacementProperty =
DependencyProperty.Register(nameof(ThumbPlacement), typeof(ThumbPlacement), typeof(ImageCropper), new PropertyMetadata(default(ThumbPlacement), OnThumbPlacementChanged));
}
}