-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathImageCropper.cs
1003 lines (816 loc) · 37.5 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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright Fela Ameghino & Contributors 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Telegram.Common;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.UI.Core;
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 Telegram.Controls
{
public enum BitmapProportions
{
Custom,
Original,
Square,
TwoOverThree,
ThreeOverFive,
ThreeOverFour,
FourOverFive,
FiveOverSeven,
NineOverSixteen,
ThreeOverTwo,
FiveOverThree,
FourOverThree,
FiveOverFour,
SevenOverFive,
SixteenOverNine,
}
public enum ImageCropperMask
{
Rectangle,
Ellipse
}
public sealed partial class ImageCropperThumb : Control
{
#region constructors
public ImageCropperThumb()
{
DefaultStyleKey = typeof(ImageCropperThumb);
}
#endregion
}
public partial class ImageCropper : ContentControl
{
private readonly Dictionary<uint, Point> m_pointerPositions;
private StorageFile m_imageSource;
private Size m_imageSize;
private Rect m_current;
private Rect m_rectangle;
private ImageCropperMask _mask = ImageCropperMask.Rectangle;
private BitmapProportions _proportions = BitmapProportions.Custom;
private BitmapRotation _rotation;
private BitmapFlip _flip;
private Grid m_layoutRoot;
private Image m_imageViewer;
private FrameworkElement m_imagePresenter;
private CompositeTransform m_imagePresenterTransform;
private Path m_clip;
private RectangleGeometry m_outerClip;
private Geometry m_innerClip;
private Grid m_thumbsContainer;
public ImageCropper()
{
DefaultStyleKey = typeof(ImageCropper);
m_pointerPositions = new Dictionary<uint, Point>();
m_current = new Rect(0, 0, 1, 1);
m_rectangle = new Rect(0, 0, 1, 1);
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
m_layoutRoot = (Grid)GetTemplateChild("LayoutRoot");
m_imageViewer = (Image)GetTemplateChild("ImageViewer");
m_imagePresenter = (FrameworkElement)GetTemplateChild("ImagePresenter");
m_thumbsContainer = (Grid)GetTemplateChild("ThumbsContainer");
m_clip = (Path)GetTemplateChild("Clip");
if (m_clip != null)
{
SetMask(_mask);
}
m_imageViewer.SizeChanged += ImageViewer_SizeChanged;
m_imagePresenter.RenderTransformOrigin = new Point(0.5, 0.5);
m_imagePresenter.RenderTransform = m_imagePresenterTransform = new CompositeTransform();
var leftThumb = (ImageCropperThumb)GetTemplateChild("LeftThumb");
if (leftThumb != null)
{
leftThumb.PointerEntered += WEThumb_PointerEntered;
leftThumb.PointerExited += Thumb_PointerExited;
leftThumb.PointerPressed += Thumb_PointerPressed;
leftThumb.PointerReleased += Thumb_PointerReleased;
leftThumb.PointerMoved += LeftThumb_PointerMoved;
}
var topThumb = (ImageCropperThumb)GetTemplateChild("TopThumb");
if (topThumb != null)
{
topThumb.PointerEntered += NSThumb_PointerEntered;
topThumb.PointerExited += Thumb_PointerExited;
topThumb.PointerPressed += Thumb_PointerPressed;
topThumb.PointerReleased += Thumb_PointerReleased;
topThumb.PointerMoved += TopThumb_PointerMoved;
}
var rightThumb = (ImageCropperThumb)GetTemplateChild("RightThumb");
if (rightThumb != null)
{
rightThumb.PointerEntered += WEThumb_PointerEntered;
rightThumb.PointerExited += Thumb_PointerExited;
rightThumb.PointerPressed += Thumb_PointerPressed;
rightThumb.PointerReleased += Thumb_PointerReleased;
rightThumb.PointerMoved += RightThumb_PointerMoved;
}
var bottomThumb = (ImageCropperThumb)GetTemplateChild("BottomThumb");
if (bottomThumb != null)
{
bottomThumb.PointerEntered += NSThumb_PointerEntered;
bottomThumb.PointerExited += Thumb_PointerExited;
bottomThumb.PointerPressed += Thumb_PointerPressed;
bottomThumb.PointerReleased += Thumb_PointerReleased;
bottomThumb.PointerMoved += BottomThumb_PointerMoved;
}
var topLeftThumb = (ImageCropperThumb)GetTemplateChild("TopLeftThumb");
if (topLeftThumb != null)
{
topLeftThumb.PointerEntered += NWSEThumb_PointerEntered;
topLeftThumb.PointerExited += Thumb_PointerExited;
topLeftThumb.PointerPressed += Thumb_PointerPressed;
topLeftThumb.PointerReleased += Thumb_PointerReleased;
topLeftThumb.PointerMoved += TopLeftThumb_PointerMoved;
}
var topRightThumb = (ImageCropperThumb)GetTemplateChild("TopRightThumb");
if (topLeftThumb != null)
{
topRightThumb.PointerEntered += NESWThumb_PointerEntered;
topRightThumb.PointerExited += Thumb_PointerExited;
topRightThumb.PointerPressed += Thumb_PointerPressed;
topRightThumb.PointerReleased += Thumb_PointerReleased;
topRightThumb.PointerMoved += TopRightThumb_PointerMoved;
}
var bottomLeftThumb = (ImageCropperThumb)GetTemplateChild("BottomLeftThumb");
if (bottomLeftThumb != null)
{
bottomLeftThumb.PointerEntered += NESWThumb_PointerEntered;
bottomLeftThumb.PointerExited += Thumb_PointerExited;
bottomLeftThumb.PointerPressed += Thumb_PointerPressed;
bottomLeftThumb.PointerReleased += Thumb_PointerReleased;
bottomLeftThumb.PointerMoved += BottomLeftThumb_PointerMoved;
}
var bottomRightThumb = (ImageCropperThumb)GetTemplateChild("BottomRightThumb");
if (bottomRightThumb != null)
{
bottomRightThumb.PointerEntered += NWSEThumb_PointerEntered;
bottomRightThumb.PointerExited += Thumb_PointerExited;
bottomRightThumb.PointerPressed += Thumb_PointerPressed;
bottomRightThumb.PointerReleased += Thumb_PointerReleased;
bottomRightThumb.PointerMoved += BottomRightThumb_PointerMoved;
}
var middleThumb = (Rectangle)GetTemplateChild("MiddleThumb");
if (middleThumb != null)
{
middleThumb.PointerPressed += Thumb_PointerPressed;
middleThumb.PointerReleased += Thumb_PointerReleased;
middleThumb.PointerMoved += MiddleThumb_PointerMoved;
}
}
private void UpdateTutteCose(Rect rect, bool animate = false)
{
m_current = rect;
if (m_thumbsContainer == null)
{
return;
}
var w = m_imageViewer.ActualWidth;
var h = m_imageViewer.ActualHeight;
if (w == 0 || h == 0)
{
return;
}
m_thumbsContainer.Margin = new Thickness(
rect.Left * w,
rect.Top * h,
w - rect.Right * w,
h - rect.Bottom * h);
//if (animate && _clipVisual != null)
//{
// var animation = BootStrapper.Current.Compositor.CreatePathKeyFrameAnimation();
// animation.InsertKeyFrame(0, _clipGeometry.Path);
// animation.InsertKeyFrame(1, GetCheckMark(_clipVisual.Size, new Rect(rect.Left * w, rect.Top * h, rect.Width * w, rect.Height * h)));
// animation.Duration = TimeSpan.FromSeconds(3);
// _clipGeometry.StartAnimation("Path", animation);
//}
//else
//{
// if (_clipVisual != null)
// {
// _clipGeometry.StopAnimation("Path");
// _clipVisual.Size = new Size(m_layoutRoot.ActualWidth, m_layoutRoot.ActualHeight).ToVector2();
// _clipGeometry.Path = GetCheckMark(_clipVisual.Size, new Rect(rect.Left * w, rect.Top * h, rect.Width * w, rect.Height * h));
// }
// else
// {
m_outerClip.Rect = new Rect(0, 0, m_imageViewer.ActualWidth, m_imageViewer.ActualHeight);
//m_innerClip.Rect = new Rect(rect.Left * w, rect.Top * h, rect.Width * w, rect.Height * h);
// }
//}
switch (m_innerClip)
{
case RectangleGeometry rectangle:
rectangle.Rect = new Rect(rect.Left * w, rect.Top * h, rect.Width * w, rect.Height * h);
break;
case EllipseGeometry ellipse:
ellipse.Center = new Point((rect.Left + rect.Width / 2) * w, (rect.Top + rect.Height / 2) * h);
ellipse.RadiusX = rect.Width / 2 * w;
ellipse.RadiusY = rect.Height / 2 * h;
break;
}
}
#region Pointer events
private void Thumb_PointerExited(object sender, PointerRoutedEventArgs e)
{
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 1);
}
private void Thumb_PointerPressed(object sender, PointerRoutedEventArgs e)
{
((UIElement)sender).CapturePointer(e.Pointer);
var pointer = e.GetCurrentPoint(m_layoutRoot);
m_pointerPositions[pointer.PointerId] = pointer.Position;
m_current = m_rectangle;
e.Handled = true;
}
private void Thumb_PointerReleased(object sender, PointerRoutedEventArgs e)
{
m_pointerPositions.Remove(e.Pointer.PointerId);
((UIElement)sender).ReleasePointerCapture(e.Pointer);
//UpdateCropRectangle(true);
m_rectangle = m_current;
e.Handled = true;
}
private void MiddleThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetX = (position.X - startPosition.X) / w;
var offsetY = (position.Y - startPosition.Y) / h;
var left = Math.Clamp(m_rectangle.Left + offsetX, 0, 1 - m_rectangle.Width);
var top = Math.Clamp(m_rectangle.Top + offsetY, 0, 1 - m_rectangle.Height);
var width = m_rectangle.Right - left;
var height = m_rectangle.Bottom - top;
var cropScale = width * w / (height * h);
var proportionalCropScale = GetProportionsFactor(BitmapProportions.Custom, cropScale);
if (cropScale < proportionalCropScale)
{
var cropHeight = width * w / proportionalCropScale / h;
m_current.X = left;
m_current.Y = top + (height - cropHeight);
}
else
{
var cropWidth = height * h * proportionalCropScale / w;
m_current.X = left + (width - cropWidth);
m_current.Y = top;
}
//m_current = new Rect(left, top, width, height);
UpdateTutteCose(m_current);
}
}
private void TopLeftThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minW = 20 / w;
var minH = 20 / h;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetX = (position.X - startPosition.X) / w;
var offsetY = (position.Y - startPosition.Y) / h;
var left = Math.Clamp(m_rectangle.Left + offsetX, 0, Math.Max(m_rectangle.Right, minW) - minW);
var top = Math.Clamp(m_rectangle.Top + offsetY, 0, Math.Max(m_rectangle.Bottom, minH) - minH);
var width = m_rectangle.Right - left;
var height = m_rectangle.Bottom - top;
var cropScale = width * w / (height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
if (cropScale < proportionalCropScale)
{
var cropHeight = width * w / proportionalCropScale / h;
m_current.X = left;
m_current.Y = top + (height - cropHeight);
m_current.Width = width;
m_current.Height = cropHeight;
}
else
{
var cropWidth = height * h * proportionalCropScale / w;
m_current.X = left + (width - cropWidth);
m_current.Y = top;
m_current.Width = cropWidth;
m_current.Height = height;
}
//m_current = new Rect(left, top, width, height);
UpdateTutteCose(m_current);
}
}
private void TopThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minH = 20 / h;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetY = (position.Y - startPosition.Y) / h;
var top = Math.Clamp(m_rectangle.Top + offsetY, 0, Math.Max(m_rectangle.Bottom, minH) - minH);
var height = m_rectangle.Bottom - top;
var cropScale = m_current.Width * w / (height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
var cropWidth = Math.Min(w, height * h * proportionalCropScale) / w;
m_current.Y = top;
m_current.X = Math.Clamp(m_current.X + (m_current.Width - cropWidth) / 2.0, 0, 1 - cropWidth);
m_current.Width = cropWidth;
m_current.Height = cropWidth * w / proportionalCropScale / h;
//m_current = new Rect(left, top, width, height);
UpdateTutteCose(m_current);
}
}
private void LeftThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minW = 20 / w;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetX = (position.X - startPosition.X) / w;
var left = Math.Clamp(m_rectangle.Left + offsetX, 0, Math.Max(m_rectangle.Right, minW) - minW);
var width = m_rectangle.Right - left;
var cropScale = width * w / (m_current.Height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
var cropHeight = Math.Min(h, width * w / proportionalCropScale) / h;
m_current.Y = Math.Clamp(m_current.Y + (m_current.Height - cropHeight) / 2.0, 0, 1 - cropHeight);
m_current.X = left;
m_current.Width = cropHeight * h * proportionalCropScale / w;
m_current.Height = cropHeight;
//m_current = new Rect(left, top, width, height);
UpdateTutteCose(m_current);
}
}
private void BottomRightThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minW = 20 / w;
var minH = 20 / h;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetX = (position.X - startPosition.X) / w;
var offsetY = (position.Y - startPosition.Y) / h;
var right = Math.Clamp(m_rectangle.Right + offsetX, m_current.Left + minW, 1);
var bottom = Math.Clamp(m_rectangle.Bottom + offsetY, m_current.Top + minH, 1);
var width = right - m_rectangle.Left;
var height = bottom - m_rectangle.Top;
var cropScale = width * w / (height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
if (cropScale < proportionalCropScale)
{
var cropHeight = width * w / proportionalCropScale / h;
m_current.Width = width;
m_current.Height = cropHeight;
}
else
{
var cropWidth = height * h * proportionalCropScale / w;
m_current.Width = cropWidth;
m_current.Height = height;
}
UpdateTutteCose(m_current);
e.Handled = true;
}
}
private void BottomThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minH = 20 / h;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetY = (position.Y - startPosition.Y) / h;
var bottom = Math.Clamp(m_rectangle.Bottom + offsetY, m_current.Top + minH, 1);
var height = bottom - m_rectangle.Top;
var cropScale = m_current.Width * w / (height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
var cropWidth = Math.Min(w, height * h * proportionalCropScale) / w;
m_current.X = Math.Clamp(m_current.X + (m_current.Width - cropWidth) / 2.0, 0, 1 - cropWidth);
m_current.Width = cropWidth;
m_current.Height = cropWidth * w / proportionalCropScale / h;
UpdateTutteCose(m_current);
e.Handled = true;
}
}
private void RightThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minW = 20 / w;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetX = (position.X - startPosition.X) / w;
var right = Math.Clamp(m_rectangle.Right + offsetX, m_current.Left + minW, 1);
var width = right - m_rectangle.Left;
var cropScale = width * w / (m_current.Height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
var cropHeight = Math.Min(h, width * w / proportionalCropScale) / h;
m_current.Y = Math.Clamp(m_current.Y + (m_current.Height - cropHeight) / 2.0, 0, 1 - cropHeight);
m_current.Width = cropHeight * h * proportionalCropScale / w;
m_current.Height = cropHeight;
UpdateTutteCose(m_current);
e.Handled = true;
}
}
private void BottomLeftThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minW = 20 / w;
var minH = 20 / h;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetX = (position.X - startPosition.X) / w;
var offsetY = (position.Y - startPosition.Y) / h;
var left = Math.Clamp(m_rectangle.Left + offsetX, 0, Math.Max(m_rectangle.Right, minW) - minW);
var bottom = Math.Clamp(m_rectangle.Bottom + offsetY, m_current.Top + minH, 1);
var width = m_rectangle.Right - left;
var height = bottom - m_rectangle.Top;
var cropScale = width * w / (height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
if (cropScale < proportionalCropScale)
{
var cropHeight = width * w / proportionalCropScale / h;
m_current.X = left;
m_current.Width = width;
m_current.Height = cropHeight;
}
else
{
var cropWidth = height * h * proportionalCropScale / w;
m_current.X = left + (width - cropWidth);
m_current.Width = cropWidth;
m_current.Height = height;
}
//m_current = new Rect(left, top, width, height);
UpdateTutteCose(m_current);
}
}
private void TopRightThumb_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.IsInContact && m_pointerPositions.TryGetValue(e.Pointer.PointerId, out Point startPosition))
{
var w = m_layoutRoot.ActualWidth;
var h = m_layoutRoot.ActualHeight;
var minW = 20 / w;
var minH = 20 / h;
var position = e.GetCurrentPoint(m_layoutRoot).Position;
var offsetX = (position.X - startPosition.X) / w;
var offsetY = (position.Y - startPosition.Y) / h;
var right = Math.Clamp(m_rectangle.Right + offsetX, m_current.Left + minW, 1);
var top = Math.Clamp(m_rectangle.Top + offsetY, 0, Math.Max(m_rectangle.Bottom, minH) - minH);
var width = right - m_rectangle.Left;
var height = m_rectangle.Bottom - top;
var cropScale = width * w / (height * h);
var proportionalCropScale = GetProportionsFactor(_proportions, cropScale);
if (cropScale < proportionalCropScale)
{
var cropHeight = width * w / proportionalCropScale / h;
m_current.Y = top + (height - cropHeight);
m_current.Width = width;
m_current.Height = cropHeight;
}
else
{
var cropWidth = height * h * proportionalCropScale / w;
m_current.Y = top;
m_current.Width = cropWidth;
m_current.Height = height;
}
//m_current = new Rect(left, top, width, height);
UpdateTutteCose(m_current);
}
}
private void NWSEThumb_PointerEntered(object sender, PointerRoutedEventArgs e)
{
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.SizeNorthwestSoutheast, 1);
}
private void NESWThumb_PointerEntered(object sender, PointerRoutedEventArgs e)
{
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.SizeNortheastSouthwest, 1);
}
private void WEThumb_PointerEntered(object sender, PointerRoutedEventArgs e)
{
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.SizeWestEast, 1);
}
private void NSThumb_PointerEntered(object sender, PointerRoutedEventArgs e)
{
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.SizeNorthSouth, 1);
}
#endregion
#region Rectangle
public void SetRectangle(Rect rectangle, bool animate = true)
{
if (rectangle.X < 0)
{
rectangle.X = 0;
}
if (rectangle.Y < 0)
{
rectangle.Y = 0;
}
if (rectangle.Right > 1)
{
rectangle.X = 1 - rectangle.Width;
}
if (rectangle.Bottom > 1)
{
rectangle.Y = 1 - rectangle.Height;
}
m_current = rectangle;
m_rectangle = rectangle;
UpdateTutteCose(rectangle, animate);
}
#endregion
#region Proportions
public void SetProportions(BitmapProportions value, bool animate = true)
{
_proportions = value;
var cropScale = m_rectangle.Width * m_imageSize.Width / (m_rectangle.Height * m_imageSize.Height);
var proportionalCropScale = GetProportionsFactor(value, cropScale);
if (cropScale < proportionalCropScale)
{
var cropWidth = m_rectangle.Height * m_imageSize.Height * proportionalCropScale / m_imageSize.Width;
if (cropWidth > 1)
{
var cropHeight = m_rectangle.Width * m_imageSize.Width / proportionalCropScale / m_imageSize.Height;
m_rectangle.Y = Math.Clamp(m_rectangle.Y + (m_rectangle.Height - cropHeight) / 2.0, 0.0, m_imageSize.Height - cropHeight);
m_rectangle.Height = cropHeight;
}
else
{
m_rectangle.X = Math.Clamp(m_rectangle.X + (m_rectangle.Width - cropWidth) / 2.0, 0.0, m_imageSize.Width - cropWidth);
m_rectangle.Width = cropWidth;
}
}
else
{
var cropHeight = m_rectangle.Width * m_imageSize.Width / proportionalCropScale / m_imageSize.Height;
if (cropHeight > 1)
{
var cropWidth = m_rectangle.Height * m_imageSize.Height * proportionalCropScale / m_imageSize.Width;
m_rectangle.X = Math.Clamp(m_rectangle.X + (m_rectangle.Width - cropWidth) / 2.0, 0.0, m_imageSize.Width - cropWidth);
m_rectangle.Width = cropWidth;
}
else
{
m_rectangle.Y = Math.Clamp(m_rectangle.Y + (m_rectangle.Height - cropHeight) / 2.0, 0.0, m_imageSize.Height - cropHeight);
m_rectangle.Height = cropHeight;
}
}
SetRectangle(m_rectangle, animate);
}
private double GetProportionsFactor(BitmapProportions proportions, double defaultValue)
{
switch (proportions)
{
case BitmapProportions.Original:
return m_imageSize.Width / m_imageSize.Height;
case BitmapProportions.Square:
return 1.0;
// Portrait
case BitmapProportions.TwoOverThree:
return 2.0 / 3.0;
case BitmapProportions.ThreeOverFive:
return 3.0 / 5.0;
case BitmapProportions.ThreeOverFour:
return 3.0 / 4.0;
case BitmapProportions.FourOverFive:
return 4.0 / 5.0;
case BitmapProportions.FiveOverSeven:
return 5.0 / 7.0;
case BitmapProportions.NineOverSixteen:
return 9.0 / 16.0;
// Landscape
case BitmapProportions.ThreeOverTwo:
return 3.0 / 2.0;
case BitmapProportions.FiveOverThree:
return 5.0 / 3.0;
case BitmapProportions.FourOverThree:
return 4.0 / 3.0;
case BitmapProportions.FiveOverFour:
return 5.0 / 4.0;
case BitmapProportions.SevenOverFive:
return 7.0 / 5.0;
case BitmapProportions.SixteenOverNine:
return 16.0 / 9.0;
default:
return defaultValue;
}
}
public IReadOnlyList<BitmapProportions> GetProportions()
{
return GetProportionsFor(m_imageSize.Width, m_imageSize.Height);
}
public static IReadOnlyList<BitmapProportions> GetProportionsFor(double width, double height)
{
if (width > height)
{
return new List<BitmapProportions>
{
BitmapProportions.Original,
BitmapProportions.Square,
BitmapProportions.ThreeOverTwo,
BitmapProportions.FiveOverThree,
BitmapProportions.FourOverThree,
BitmapProportions.FiveOverFour,
BitmapProportions.SevenOverFive,
BitmapProportions.SixteenOverNine
};
}
return new List<BitmapProportions>
{
BitmapProportions.Original,
BitmapProportions.Square,
BitmapProportions.TwoOverThree,
BitmapProportions.ThreeOverFive,
BitmapProportions.ThreeOverFour,
BitmapProportions.FourOverFive,
BitmapProportions.FiveOverSeven,
BitmapProportions.NineOverSixteen
};
}
#endregion
#region Mask
public void SetMask(ImageCropperMask mask)
{
_mask = mask;
var clip = m_clip;
if (clip == null)
{
return;
}
var group = new GeometryGroup();
group.Children.Add(m_outerClip = new RectangleGeometry());
group.Children.Add(m_innerClip = mask == ImageCropperMask.Rectangle
? new RectangleGeometry()
: new EllipseGeometry());
clip.Data = group;
UpdateTutteCose(m_rectangle);
}
#endregion
#region Properties
public int PixelWidth => (int)m_imageSize.Width;
public int PixelHeight => (int)m_imageSize.Height;
public BitmapProportions Proportions => _proportions;
public Rect CropRectangle => m_rectangle;
private bool _isCropEnabled = true;
public bool IsCropEnabled
{
get => _isCropEnabled;
set
{
_isCropEnabled = value;
VisualStateManager.GoToState(this, value ? "Normal" : "Disabled", true);
}
}
public ImageCropperMask Mask => _mask;
#endregion
#region Source
public async Task SetSourceAsync(StorageFile file, BitmapRotation rotation = BitmapRotation.None, BitmapFlip flip = BitmapFlip.None, BitmapProportions proportions = BitmapProportions.Custom, Rect? cropRectangle = null)
{
_rotation = rotation;
_flip = flip;
try
{
SoftwareBitmapSource source;
using (var fileStream = await ImageHelper.OpenReadAsync(file))
{
var decoder = await BitmapDecoder.CreateAsync(fileStream);
var transform = ImageHelper.ComputeScalingTransformForSourceImage(decoder);
transform.Rotation = rotation;
transform.Flip = flip;
var software = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
source = new SoftwareBitmapSource();
await source.SetBitmapAsync(software);
SetSource(file, source, software.PixelWidth, software.PixelHeight, proportions, cropRectangle);
}
UpdatePresenterTransform();
}
catch
{
// TODO: decide how to handle failures in opening the image
}
}
public void SetSource(StorageFile file, ImageSource source, double width, double height, BitmapProportions proportions = BitmapProportions.Custom, Rect? cropRectangle = null)
{
m_imageViewer.Source = source;
//m_imagePreview = source;
m_imageSource = file;
m_imageSize = new Size(width, height);
if (cropRectangle is Rect rectangle)
{
SetProportions(proportions, false);
SetRectangle(rectangle, false);
}
else
{
SetRectangle(new Rect(0, 0, 1, 1), false);
SetProportions(proportions, false);
}
//m_suppressUpdates = true;
//Proportions = proportions;
//if (m_imageViewer != null)
//{
// m_imageViewer.Source = m_imagePreview;
// Canvas.SetLeft(m_imageThumb, (m_layoutRoot.ActualWidth - m_imageSize.Width) / 2.0);
// Canvas.SetTop(m_imageThumb, (m_layoutRoot.ActualHeight - m_imageSize.Height) / 2.0);
// var imageScale = m_imageSize.Width / m_imageSize.Height;
// var cropScale = GetProportionsFactor(Proportions, imageScale);
// if (imageScale < cropScale)
// {
// var cropHeight = m_imageSize.Width / cropScale;
// m_cropRectangle = new Rect(0.0, (m_imageSize.Height - cropHeight) / 2.0, m_imageSize.Width, cropHeight);
// }
// else
// {
// var cropWidth = m_imageSize.Height * cropScale;
// m_cropRectangle = new Rect((m_imageSize.Width - cropWidth) / 2.0, 0.0, cropWidth, m_imageSize.Height);
// }
// if (cropRectangle != null)
// {
// m_cropRectangle = cropRectangle.Value;
// }
// UpdateCropRectangle(m_cropRectangle, false);
//}
//else
//{
// m_imageWaiting = true;
//}
}
public async Task<StorageFile> CropAsync(int min = 1280, int max = 0)
{
return await ImageHelper.CropAsync(m_imageSource, null, m_rectangle, min, max, rotation: _rotation, flip: _flip);
}
#endregion
#region Content
private void ImageViewer_SizeChanged(object sender, SizeChangedEventArgs e)
{
var horizontal = -e.NewSize.Height;
var vertical = -e.NewSize.Width;
//if (e.NewSize.Width > e.NewSize.Height)
//{
// horizontal = 0;
//}
//else
//{
// vertical = 0;
//}
switch (_rotation)
{
case BitmapRotation.None:
case BitmapRotation.Clockwise180Degrees:
m_imagePresenter.Margin = new Thickness(horizontal, vertical, horizontal, vertical);
m_imagePresenter.Width = e.NewSize.Width;
m_imagePresenter.Height = e.NewSize.Height;
m_imagePresenterTransform.Rotation = _rotation == BitmapRotation.None
? 0
: 180;
break;
case BitmapRotation.Clockwise90Degrees:
case BitmapRotation.Clockwise270Degrees:
m_imagePresenter.Margin = new Thickness(horizontal, vertical, horizontal, vertical);
m_imagePresenter.Width = e.NewSize.Height;
m_imagePresenter.Height = e.NewSize.Width;
m_imagePresenterTransform.Rotation = _rotation == BitmapRotation.Clockwise90Degrees
? 90
: 270;
break;
}
UpdatePresenterTransform();
UpdateTutteCose(m_rectangle);
}
private void UpdatePresenterTransform()
{
m_imagePresenterTransform.ScaleX = _flip == BitmapFlip.Horizontal
? -1
: 1;
m_imagePresenterTransform.ScaleY = _flip == BitmapFlip.Vertical
? -1
: 1;
}
#endregion