CropFunctions.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Microsoft.Win32;
  2. using PicView.FileHandling;
  3. using PicView.ImageHandling;
  4. using PicView.UILogic;
  5. using PicView.UILogic.Loading;
  6. using System;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using static PicView.ChangeImage.Navigation;
  13. using static PicView.UILogic.Sizing.ScaleImage;
  14. using static PicView.UILogic.TransformImage.Rotation;
  15. using static PicView.UILogic.UC;
  16. namespace PicView.Editing.Crop
  17. {
  18. internal static class CropFunctions
  19. {
  20. internal static CropService? CropService { get; private set; }
  21. internal static void StartCrop()
  22. {
  23. if (ConfigureWindows.GetMainWindow.MainImage.Source == null) { return; }
  24. if (GetCropppingTool == null)
  25. {
  26. LoadControls.LoadCroppingTool();
  27. }
  28. GetCropppingTool.Width = Rotateint == 0 || Rotateint == 180 ? XWidth : XHeight;
  29. GetCropppingTool.Height = Rotateint == 0 || Rotateint == 180 ? XHeight : XWidth;
  30. ConfigureWindows.GetMainWindow.TitleText.Text = (string)Application.Current.Resources["CropMessage"];
  31. if (!ConfigureWindows.GetMainWindow.ParentContainer.Children.Contains(GetCropppingTool))
  32. {
  33. ConfigureWindows.GetMainWindow.ParentContainer.Children.Add(GetCropppingTool);
  34. }
  35. CanNavigate = false;
  36. }
  37. internal static async Task PerformCropAsync()
  38. {
  39. await SaveCrop().ConfigureAwait(false);
  40. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(() =>
  41. {
  42. if (Pics.Count == 0)
  43. {
  44. SetTitle.SetTitleString((int)ConfigureWindows.GetMainWindow.MainImage.Source.Width, (int)ConfigureWindows.GetMainWindow.MainImage.Source.Height);
  45. }
  46. else
  47. {
  48. SetTitle.SetTitleString((int)ConfigureWindows.GetMainWindow.MainImage.Source.Width, (int)ConfigureWindows.GetMainWindow.MainImage.Source.Height, FolderIndex);
  49. }
  50. });
  51. CanNavigate = true;
  52. }
  53. internal static void CloseCrop()
  54. {
  55. if (Pics.Count == 0)
  56. {
  57. SetTitle.SetTitleString((int)ConfigureWindows.GetMainWindow.MainImage.Source.Width, (int)ConfigureWindows.GetMainWindow.MainImage.Source.Height);
  58. }
  59. else
  60. {
  61. SetTitle.SetTitleString((int)ConfigureWindows.GetMainWindow.MainImage.Source.Width, (int)ConfigureWindows.GetMainWindow.MainImage.Source.Height, FolderIndex);
  62. }
  63. ConfigureWindows.GetMainWindow.ParentContainer.Children.Remove(GetCropppingTool);
  64. CanNavigate = true;
  65. }
  66. internal static void InitilizeCrop()
  67. {
  68. GetCropppingTool.Width = Rotateint == 0 || Rotateint == 180 ? XWidth : XHeight;
  69. GetCropppingTool.Height = Rotateint == 0 || Rotateint == 180 ? XHeight : XWidth;
  70. CropService = new CropService(GetCropppingTool);
  71. var chosenColorBrush = Application.Current.Resources["ChosenColorBrush"] as SolidColorBrush;
  72. GetCropppingTool.RootGrid.Background =
  73. new SolidColorBrush(Color.FromArgb(
  74. 25,
  75. chosenColorBrush.Color.R,
  76. chosenColorBrush.Color.G,
  77. chosenColorBrush.Color.B
  78. ));
  79. GetCropppingTool.RootGrid.PreviewMouseDown += (s, e) => CropService.Adorner.RaiseEvent(e);
  80. GetCropppingTool.RootGrid.PreviewMouseLeftButtonUp += (s, e) => CropService.Adorner.RaiseEvent(e);
  81. }
  82. internal static async Task SaveCrop()
  83. {
  84. var fileName = Pics.Count == 0 ? Path.GetRandomFileName()
  85. : Path.GetFileName(Pics[FolderIndex]);
  86. var Savedlg = new SaveFileDialog
  87. {
  88. Filter = Open_Save.FilterFiles,
  89. Title = $"{Application.Current.Resources["SaveImage"]} - {SetTitle.AppName}",
  90. FileName = fileName
  91. };
  92. if (!Savedlg.ShowDialog().Value)
  93. {
  94. return;
  95. }
  96. Open_Save.IsDialogOpen = true;
  97. var crop = GetCrop();
  98. var success = false;
  99. if (Pics.Count > 0)
  100. {
  101. await Task.Run(() =>
  102. success = SaveImages.TrySaveImage(
  103. crop,
  104. Pics[FolderIndex],
  105. Savedlg.FileName)).ConfigureAwait(false);
  106. }
  107. else
  108. {
  109. // Fixes saving if from web
  110. // TODO add working method for copied images
  111. var source = ConfigureWindows.GetMainWindow.MainImage.Source as BitmapSource;
  112. await Task.Run(() =>
  113. success = SaveImages.TrySaveImage(
  114. crop,
  115. source,
  116. Savedlg.FileName)).ConfigureAwait(false);
  117. }
  118. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(() =>
  119. {
  120. if (success == false)
  121. {
  122. Tooltip.ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
  123. }
  124. ConfigureWindows.GetMainWindow.ParentContainer.Children.Remove(GetCropppingTool);
  125. });
  126. }
  127. internal static Int32Rect GetCrop()
  128. {
  129. var cropArea = CropService.GetCroppedArea();
  130. int x, y, width, height;
  131. if (AspectRatio != 0)
  132. {
  133. if (Rotateint == 0 || Rotateint == 180)
  134. {
  135. x = Convert.ToInt32(cropArea.CroppedRectAbsolute.X / AspectRatio);
  136. y = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y / AspectRatio);
  137. width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width / AspectRatio);
  138. height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height / AspectRatio);
  139. }
  140. else
  141. {
  142. x = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y / AspectRatio);
  143. y = Convert.ToInt32(cropArea.CroppedRectAbsolute.X / AspectRatio);
  144. width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height / AspectRatio);
  145. height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width / AspectRatio);
  146. }
  147. }
  148. else
  149. {
  150. if (Rotateint == 0 || Rotateint == 180)
  151. {
  152. x = Convert.ToInt32(cropArea.CroppedRectAbsolute.X);
  153. y = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y);
  154. width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width);
  155. height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height);
  156. }
  157. else
  158. {
  159. x = Convert.ToInt32(cropArea.CroppedRectAbsolute.Y);
  160. y = Convert.ToInt32(cropArea.CroppedRectAbsolute.X);
  161. width = Convert.ToInt32(cropArea.CroppedRectAbsolute.Height);
  162. height = Convert.ToInt32(cropArea.CroppedRectAbsolute.Width);
  163. }
  164. }
  165. return new Int32Rect(x, y, width, height);
  166. }
  167. }
  168. }