forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCropper.cs
450 lines (399 loc) · 18.3 KB
/
ImageCropper.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// 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 System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Shapes;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// The <see cref="ImageCropper"/> control allows user to crop image freely.
/// </summary>
[TemplatePart(Name = LayoutGridName, Type = typeof(Grid))]
[TemplatePart(Name = ImageCanvasPartName, Type = typeof(Canvas))]
[TemplatePart(Name = SourceImagePartName, Type = typeof(Image))]
[TemplatePart(Name = MaskAreaPathPartName, Type = typeof(Path))]
[TemplatePart(Name = TopThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = BottomThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = LeftThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = RightThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = UpperLeftThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = UpperRightThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = LowerLeftThumbPartName, Type = typeof(ImageCropperThumb))]
[TemplatePart(Name = LowerRightThumbPartName, Type = typeof(ImageCropperThumb))]
public partial class ImageCropper : Control
{
private readonly CompositeTransform _imageTransform = new CompositeTransform();
private readonly CompositeTransform _inverseImageTransform = new CompositeTransform();
private readonly GeometryGroup _maskAreaGeometryGroup = new GeometryGroup { FillRule = FillRule.EvenOdd };
private Grid _layoutGrid;
private Canvas _imageCanvas;
private Image _sourceImage;
private Path _maskAreaPath;
private ImageCropperThumb _topThumb;
private ImageCropperThumb _bottomThumb;
private ImageCropperThumb _leftThumb;
private ImageCropperThumb _rightThumb;
private ImageCropperThumb _upperLeftThumb;
private ImageCropperThumb _upperRightThumb;
private ImageCropperThumb _lowerLeftThumb;
private ImageCropperThumb _lowerRigthThumb;
private double _startX;
private double _startY;
private double _endX;
private double _endY;
private Rect _restrictedCropRect = Rect.Empty;
private Rect _restrictedSelectRect = Rect.Empty;
private RectangleGeometry _outerGeometry;
private Geometry _innerGeometry;
private TimeSpan _animationDuration = TimeSpan.FromSeconds(0.3);
/// <summary>
/// Initializes a new instance of the <see cref="ImageCropper"/> class.
/// </summary>
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;
private double UsedAspectRatio
{
get
{
var aspectRatio = AspectRatio;
switch (CropShape)
{
case CropShape.Rectangular:
break;
case CropShape.Circular:
aspectRatio = 1;
break;
}
return aspectRatio != null && aspectRatio > 0 ? aspectRatio.Value : -1;
}
}
/// <summary>
/// Gets the minimum cropped size.
/// </summary>
private Size MinCropSize
{
get
{
var aspectRatio = KeepAspectRatio ? UsedAspectRatio : 1;
var size = new Size(MinCroppedPixelLength, MinCroppedPixelLength);
if (aspectRatio >= 1)
{
size.Width = size.Height * aspectRatio;
}
else
{
size.Height = size.Width / aspectRatio;
}
return size;
}
}
/// <summary>
/// Gets the minimum selectable size.
/// </summary>
private Size MinSelectSize
{
get
{
var realMinSelectSize = _imageTransform.TransformBounds(new Rect(default(Point), MinCropSize));
var minLength = Math.Min(realMinSelectSize.Width, realMinSelectSize.Height);
if (minLength < MinSelectedLength)
{
var aspectRatio = KeepAspectRatio ? UsedAspectRatio : 1;
var minSelectSize = new Size(MinSelectedLength, MinSelectedLength);
if (aspectRatio >= 1)
{
minSelectSize.Width = minSelectSize.Height * aspectRatio;
}
else
{
minSelectSize.Height = minSelectSize.Width / aspectRatio;
}
return minSelectSize;
}
return new Size(realMinSelectSize.Width, realMinSelectSize.Height);
}
}
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
UnhookEvents();
_layoutGrid = GetTemplateChild(LayoutGridName) as Grid;
_imageCanvas = GetTemplateChild(ImageCanvasPartName) as Canvas;
_sourceImage = GetTemplateChild(SourceImagePartName) as Image;
_maskAreaPath = GetTemplateChild(MaskAreaPathPartName) as Path;
_topThumb = GetTemplateChild(TopThumbPartName) as ImageCropperThumb;
_bottomThumb = GetTemplateChild(BottomThumbPartName) as ImageCropperThumb;
_leftThumb = GetTemplateChild(LeftThumbPartName) as ImageCropperThumb;
_rightThumb = GetTemplateChild(RightThumbPartName) as ImageCropperThumb;
_upperLeftThumb = GetTemplateChild(UpperLeftThumbPartName) as ImageCropperThumb;
_upperRightThumb = GetTemplateChild(UpperRightThumbPartName) as ImageCropperThumb;
_lowerLeftThumb = GetTemplateChild(LowerLeftThumbPartName) as ImageCropperThumb;
_lowerRigthThumb = GetTemplateChild(LowerRightThumbPartName) as ImageCropperThumb;
HookUpEvents();
UpdateThumbsVisibility();
}
private void HookUpEvents()
{
if (_imageCanvas != null)
{
_imageCanvas.SizeChanged += ImageCanvas_SizeChanged;
}
if (_sourceImage != null)
{
_sourceImage.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
_sourceImage.ManipulationDelta += SourceImage_ManipulationDelta;
}
if (_maskAreaPath != null)
{
_maskAreaPath.Data = _maskAreaGeometryGroup;
}
if (_topThumb != null)
{
_topThumb.Position = ThumbPosition.Top;
_topThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_topThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_topThumb.KeyDown += ImageCropperThumb_KeyDown;
_topThumb.KeyUp += ImageCropperThumb_KeyUp;
}
if (_bottomThumb != null)
{
_bottomThumb.Position = ThumbPosition.Bottom;
_bottomThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_bottomThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_bottomThumb.KeyDown += ImageCropperThumb_KeyDown;
_bottomThumb.KeyUp += ImageCropperThumb_KeyUp;
}
if (_leftThumb != null)
{
_leftThumb.Position = ThumbPosition.Left;
_leftThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_leftThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_leftThumb.KeyDown += ImageCropperThumb_KeyDown;
_leftThumb.KeyUp += ImageCropperThumb_KeyUp;
}
if (_rightThumb != null)
{
_rightThumb.Position = ThumbPosition.Right;
_rightThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_rightThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_rightThumb.KeyDown += ImageCropperThumb_KeyDown;
_rightThumb.KeyUp += ImageCropperThumb_KeyUp;
}
if (_upperLeftThumb != null)
{
_upperLeftThumb.Position = ThumbPosition.UpperLeft;
_upperLeftThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_upperLeftThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_upperLeftThumb.KeyDown += ImageCropperThumb_KeyDown;
_upperLeftThumb.KeyUp += ImageCropperThumb_KeyUp;
}
if (_upperRightThumb != null)
{
_upperRightThumb.Position = ThumbPosition.UpperRight;
_upperRightThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_upperRightThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_upperRightThumb.KeyDown += ImageCropperThumb_KeyDown;
_upperRightThumb.KeyUp += ImageCropperThumb_KeyUp;
}
if (_lowerLeftThumb != null)
{
_lowerLeftThumb.Position = ThumbPosition.LowerLeft;
_lowerLeftThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_lowerLeftThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_lowerLeftThumb.KeyDown += ImageCropperThumb_KeyDown;
_lowerLeftThumb.KeyUp += ImageCropperThumb_KeyUp;
}
if (_lowerRigthThumb != null)
{
_lowerRigthThumb.Position = ThumbPosition.LowerRight;
_lowerRigthThumb.ManipulationDelta += ImageCropperThumb_ManipulationDelta;
_lowerRigthThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_lowerRigthThumb.KeyDown += ImageCropperThumb_KeyDown;
_lowerRigthThumb.KeyUp += ImageCropperThumb_KeyUp;
}
}
private void UnhookEvents()
{
if (_imageCanvas != null)
{
_imageCanvas.SizeChanged -= ImageCanvas_SizeChanged;
}
if (_sourceImage != null)
{
_sourceImage.ManipulationDelta -= SourceImage_ManipulationDelta;
}
if (_maskAreaPath != null)
{
_maskAreaPath.Data = null;
}
if (_topThumb != null)
{
_topThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_topThumb.ManipulationCompleted -= ImageCropperThumb_ManipulationCompleted;
_topThumb.KeyDown -= ImageCropperThumb_KeyDown;
_topThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
if (_bottomThumb != null)
{
_bottomThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_bottomThumb.ManipulationCompleted -= ImageCropperThumb_ManipulationCompleted;
_bottomThumb.KeyDown -= ImageCropperThumb_KeyDown;
_bottomThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
if (_leftThumb != null)
{
_leftThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_leftThumb.ManipulationCompleted += ImageCropperThumb_ManipulationCompleted;
_leftThumb.KeyDown -= ImageCropperThumb_KeyDown;
_leftThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
if (_rightThumb != null)
{
_rightThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_rightThumb.ManipulationCompleted -= ImageCropperThumb_ManipulationCompleted;
_rightThumb.KeyDown -= ImageCropperThumb_KeyDown;
_rightThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
if (_upperLeftThumb != null)
{
_upperLeftThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_upperLeftThumb.ManipulationCompleted -= ImageCropperThumb_ManipulationCompleted;
_upperLeftThumb.KeyDown -= ImageCropperThumb_KeyDown;
_upperLeftThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
if (_upperRightThumb != null)
{
_upperRightThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_upperRightThumb.ManipulationCompleted -= ImageCropperThumb_ManipulationCompleted;
_upperRightThumb.KeyDown -= ImageCropperThumb_KeyDown;
_upperRightThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
if (_lowerLeftThumb != null)
{
_lowerLeftThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_lowerLeftThumb.ManipulationCompleted -= ImageCropperThumb_ManipulationCompleted;
_lowerLeftThumb.KeyDown -= ImageCropperThumb_KeyDown;
_lowerLeftThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
if (_lowerRigthThumb != null)
{
_lowerRigthThumb.ManipulationDelta -= ImageCropperThumb_ManipulationDelta;
_lowerRigthThumb.ManipulationCompleted -= ImageCropperThumb_ManipulationCompleted;
_lowerRigthThumb.KeyDown -= ImageCropperThumb_KeyDown;
_lowerRigthThumb.KeyUp -= ImageCropperThumb_KeyUp;
}
}
/// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize)
{
if (Source == null || Source.PixelWidth == 0 || Source.PixelHeight == 0)
{
return base.MeasureOverride(availableSize);
}
if (double.IsInfinity(availableSize.Width) || double.IsInfinity(availableSize.Height))
{
if (!double.IsInfinity(availableSize.Width))
{
availableSize.Height = availableSize.Width / Source.PixelWidth * Source.PixelHeight;
}
else if (!double.IsInfinity(availableSize.Height))
{
availableSize.Width = availableSize.Height / Source.PixelHeight * Source.PixelWidth;
}
else
{
availableSize.Width = Source.PixelWidth;
availableSize.Height = Source.PixelHeight;
}
base.MeasureOverride(availableSize);
return availableSize;
}
return base.MeasureOverride(availableSize);
}
/// <summary>
/// Load an image from a file.
/// </summary>
/// <param name="imageFile">The image file.</param>
/// <returns>Task</returns>
public async Task LoadImageFromFile(StorageFile imageFile)
{
var writeableBitmap = new WriteableBitmap(1, 1);
using (var stream = await imageFile.OpenReadAsync())
{
await writeableBitmap.SetSourceAsync(stream);
}
Source = writeableBitmap;
}
/// <summary>
/// Saves the cropped image to a stream with the specified format.
/// </summary>
/// <param name="stream">The target stream.</param>
/// <param name="bitmapFileFormat">the specified format.</param>
/// <param name="keepRectangularOutput">Whether to always keep rectangular output.</param>
/// <returns>Task</returns>
public async Task SaveAsync(IRandomAccessStream stream, BitmapFileFormat bitmapFileFormat, bool keepRectangularOutput = false)
{
if (Source == null)
{
return;
}
if (keepRectangularOutput || CropShape == CropShape.Rectangular)
{
await CropImageAsync(Source, stream, CroppedRegion, bitmapFileFormat);
return;
}
await CropImageWithShapeAsync(Source, stream, CroppedRegion, bitmapFileFormat, CropShape);
}
/// <summary>
/// Reset the cropped area.
/// </summary>
public void Reset()
{
InitImageLayout(true);
}
/// <summary>
/// Tries to set a new value for the cropped region, returns true if it succeeded, false if the region is invalid
/// </summary>
/// <param name="rect">The new cropped region.</param>
/// <returns>bool</returns>
public bool TrySetCroppedRegion(Rect rect)
{
// Reject regions smaller than the minimum size
if (rect.Width < MinCropSize.Width || rect.Height < MinCropSize.Height)
{
return false;
}
// Reject regions that are not contained in the original picture
if (rect.Left < _restrictedCropRect.Left || rect.Top < _restrictedCropRect.Top || rect.Right > _restrictedCropRect.Right || rect.Bottom > _restrictedCropRect.Bottom)
{
return false;
}
// If an aspect ratio is set, reject regions that don't respect it
// If cropping a circle, reject regions where the aspect ratio is not 1
if (KeepAspectRatio && UsedAspectRatio != rect.Width / rect.Height)
{
return false;
}
CroppedRegion = rect;
UpdateImageLayout(true);
return true;
}
}
}