SaveImageHandler.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using PicView.Avalonia.Navigation;
  2. using PicView.Avalonia.UI;
  3. using PicView.Avalonia.ViewModels;
  4. using PicView.Core.ImageDecoding;
  5. using PicView.Core.Localization;
  6. namespace PicView.Avalonia.ImageHandling;
  7. public static class SaveImageHandler
  8. {
  9. public static async Task SaveImageWithPossibleNavigation(MainViewModel vm,
  10. string path,
  11. string destination,
  12. bool sameFile,
  13. string ext,
  14. uint? width,
  15. uint? height,
  16. uint? quality,
  17. uint? rotationAngle,
  18. bool isKeepingAspectRatio)
  19. {
  20. var success = await SaveImageFileHelper.SaveImageAsync(
  21. null, path, sameFile ? null : destination, width, height, quality,
  22. ext, rotationAngle, null, isKeepingAspectRatio).ConfigureAwait(false);
  23. if (!success)
  24. {
  25. await TooltipHelper.ShowTooltipMessageAsync(TranslationManager.Translation.SavingFileFailed);
  26. return;
  27. }
  28. if (Path.GetExtension(path) != ext && sameFile)
  29. {
  30. // Delete the old file
  31. await vm.PlatformService.DeleteFile(path, true);
  32. }
  33. if (destination == path)
  34. {
  35. await NavigationManager.QuickReload().ConfigureAwait(false);
  36. }
  37. else if (Path.GetDirectoryName(path) == Path.GetDirectoryName(destination))
  38. {
  39. // Load the file if saved within same directory
  40. await NavigationManager.LoadPicFromFile(destination, vm).ConfigureAwait(false);
  41. }
  42. }
  43. }