|
@@ -2,6 +2,8 @@
|
|
|
using Avalonia;
|
|
|
using Avalonia.Media.Imaging;
|
|
|
using PicView.Avalonia.Crop;
|
|
|
+using PicView.Avalonia.FileSystem;
|
|
|
+using PicView.Avalonia.Navigation;
|
|
|
using PicView.Avalonia.UI;
|
|
|
using PicView.Core.Localization;
|
|
|
using ReactiveUI;
|
|
@@ -15,6 +17,8 @@ public class ImageCropperViewModel : ViewModelBase
|
|
|
Bitmap = bitmap;
|
|
|
CropImageCommand = ReactiveCommand.CreateFromTask(async () =>
|
|
|
{
|
|
|
+ var croppedBitmap = GetCroppedBitmap();
|
|
|
+ await SaveCroppedImageAsync(croppedBitmap);
|
|
|
|
|
|
});
|
|
|
CloseCropCommand = ReactiveCommand.Create(() =>
|
|
@@ -73,12 +77,45 @@ public class ImageCropperViewModel : ViewModelBase
|
|
|
get;
|
|
|
set => this.RaiseAndSetIfChanged(ref field, value);
|
|
|
}
|
|
|
+
|
|
|
+ public double AspectRatio
|
|
|
+ {
|
|
|
+ get;
|
|
|
+ init => this.RaiseAndSetIfChanged(ref field, value);
|
|
|
+ }
|
|
|
|
|
|
- // Call this method when the user completes the selection
|
|
|
public CroppedBitmap GetCroppedBitmap()
|
|
|
{
|
|
|
var sourceRect = new PixelRect((int)SelectionX, (int)SelectionY, (int)SelectionWidth, (int)SelectionHeight);
|
|
|
var croppedBitmap = new CroppedBitmap(Bitmap, sourceRect);
|
|
|
return croppedBitmap;
|
|
|
}
|
|
|
+
|
|
|
+ private async Task SaveCroppedImageAsync(CroppedBitmap croppedBitmap)
|
|
|
+ {
|
|
|
+ if (UIHelper.GetMainView.DataContext is not MainViewModel vm)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ string fileName;
|
|
|
+ FileInfo fileInfo;
|
|
|
+ if (!NavigationHelper.CanNavigate(vm))
|
|
|
+ {
|
|
|
+ var random = new Random();
|
|
|
+ fileName = $"{TranslationHelper.Translation.Crop} {random.Next(9999)}.png";
|
|
|
+ fileInfo = new FileInfo(fileName);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ fileName = vm.FileInfo.FullName;
|
|
|
+ fileInfo = vm.FileInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ var saveFileDialog = await FilePicker.PickFileForSavingAsync(fileName);
|
|
|
+ if (saveFileDialog is null)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|