Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ImageCroppingViewModel.cs
1using CommunityToolkit.Mvvm.ComponentModel;
2using CommunityToolkit.Mvvm.Input;
4using System.Threading.Tasks;
6
8{
14 {
18 private readonly ImageCroppingNavigationArgs? args;
19
24 [ObservableProperty]
25 private ImageCropperView? imageCropperView;
26
32 {
33 this.args = args;
34 }
35
41 public override async Task OnAppearingAsync()
42 {
43 await base.OnAppearingAsync();
44
45 if (this.args?.Source is not null && this.ImageCropperView is not null)
46 {
47 // Assign the incoming source to the cropper control.
48 this.ImageCropperView.ImageSource = this.args.Source;
49 }
50
51 if (this.args?.OutputResolution is not null && this.ImageCropperView is not null)
52 {
53 this.ImageCropperView.OutputMaxResolution = this.args.OutputResolution.Value;
54 }
55
56 if (this.args?.CropMode is not null && this.ImageCropperView is not null)
57 {
58 this.ImageCropperView.CropMode = this.args.CropMode;
59 }
60 }
61
66 [RelayCommand]
67 private async Task CropAsync()
68 {
69 if (this.ImageCropperView is null || this.args is null)
70 return;
71
72 //Run the crop operation on a background thread.
73 await Task.Run(() =>
74 {
75 byte[]? CroppedResult = this.ImageCropperView.PerformCrop();
76 this.args.CompletionSource?.TrySetResult(CroppedResult);
77 });
78 // Then pop navigation.
79 await this.GoBack();
80 }
81
86 [RelayCommand]
87 private async Task CancelAsync()
88 {
89 this.args?.CompletionSource?.SetResult(null);
90 await this.GoBack();
91 }
92
97 [RelayCommand]
98 private Task RotateAsync()
99 {
100 if (this.ImageCropperView is not null)
101 {
102 // Increment the rotation angle by 90 degrees.
103 double NewRotation = this.ImageCropperView.RotationAngle + 90;
104 // Normalize the rotation angle to stay within 0-359 degrees.
105 this.ImageCropperView.RotationAngle = NewRotation % 360;
106 }
107 return Task.CompletedTask;
108 }
109 }
110}
A custom view that displays an image with pinch-zoom, pan, and rotation support, and allows cropping ...
byte?[] PerformCrop()
Performs cropping of the image using the current pan, zoom, and rotation transformations....
A base class for all view models, inheriting from the BindableObject. NOTE: using this class requir...
virtual async Task GoBack()
Method called when user wants to navigate to the previous screen.
Holds navigation parameters for opening the Image Cropping page.
The ViewModel for ImageCroppingPage. Handles cropping commands and navigation logic.
ImageCroppingViewModel(ImageCroppingNavigationArgs? args)
Initializes a new instance of the ImageCroppingViewModel class.
override async Task OnAppearingAsync()
Called when the page appears. Assigns the incoming source to the cropper control.
CropMode
Specifies how the image should be cropped.