3using System.Threading.Tasks;
4using Microsoft.Maui.Storage;
8using Microsoft.Maui.Controls;
9using CommunityToolkit.Mvvm.Input;
11using CommunityToolkit.Mvvm.ComponentModel;
17 public override string? StringValue
19 get => this.RawValue as string;
21 this.RawValue = value;
22 this.ImageSource = ImageSource.FromStream(() =>
new MemoryStream(Convert.FromBase64String(
this.RawValue as
string ??
string.Empty)));
26 public int? TargetWidth
31 return this.
Metadata.TryGetValue(
"TargetWidth", out Value) ? Value as
int? :
null;
34 public int? TargetHeight
39 return this.
Metadata.TryGetValue(
"TargetHeight", out Value) ? Value as
int? :
null;
42 public double? AspectRatio
47 return this.
Metadata.TryGetValue(
"AspectRatio", out Value) ? Value as
double? :
null;
50 public bool? ShouldCrop
55 return this.
Metadata.TryGetValue(
"ShouldCrop", out Value) ? Value as
bool? :
null;
60 private ImageSource? imageSource;
63 private bool allowUpload =
true;
66 private async Task PickPhoto()
70 FileResult? FileResult = await MediaPicker.Default.PickPhotoAsync(
new MediaPickerOptions()
75 if (FileResult is
null)
78 Stream FileStream = await FileResult.OpenReadAsync();
79 byte[] InputBin = await ToByteArrayAsync(FileStream) ??
throw new Exception(
"Failed to read photo stream");
81 TaskCompletionSource<byte[]?> Tcs =
new();
87 byte[] OutputBin = await Tcs.Task ??
throw new Exception(
"Failed to crop photo");
88 this.RawValue = Convert.ToBase64String(OutputBin);
90 this.ImageSource = ImageSource.FromStream(() =>
new MemoryStream(Convert.FromBase64String(
this.RawValue as
string ??
string.Empty)));
100 private async Task TakePhoto()
109 FileResult? FileResult = await MediaPicker.Default.CapturePhotoAsync(
new MediaPickerOptions()
114 if (FileResult is
null)
117 Stream FileStream = await FileResult.OpenReadAsync();
119 byte[] InputBin = await ToByteArrayAsync(FileStream) ??
throw new Exception(
"Failed to read photo stream");
121 TaskCompletionSource<byte[]?> Tcs =
new();
124 byte[] OutputBin = await Tcs.Task ??
throw new Exception(
"Failed to crop photo");
126 this.RawValue = Convert.ToBase64String(OutputBin);
128 this.ImageSource = ImageSource.FromStream(() =>
new MemoryStream(Convert.FromBase64String(
this.RawValue as
string ??
string.Empty)));
139 private static async Task<byte[]?> ToByteArrayAsync(
System.IO.Stream stream)
143 using (System.IO.MemoryStream MemoryStream =
new System.IO.MemoryStream())
145 await stream.CopyToAsync(MemoryStream);
146 return MemoryStream.ToArray();
A strongly-typed resource class, for looking up localized strings, etc.
static string TakePhotoOfYourself
Looks up a localized string similar to Take a Photo of yourself.
static string FailedToLoadPhoto
Looks up a localized string similar to Failed to load photo.
static string ErrorTitle
Looks up a localized string similar to An error has occurred.
static string PickPhotoOfYourself
Looks up a localized string similar to Pick a photo of yourself.
Dictionary< string, object?> Metadata
Arbitrary metadata for field-specific extensions. Use e.g. Metadata["TargetWidth"] or Metadata["MaxFi...
Base class that references services in the app.
static ILogService LogService
Log service.
static IUiService UiService
Service serializing and managing UI-related tasks.
static INavigationService NavigationService
The navigation service for navigating between pages.
static IReportingStringLocalizer Localizer
Localization service
static IPermissionService PermissionService
Permission Service
Holds navigation parameters for opening the Image Cropping page.