Open_Save.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using Microsoft.Win32;
  2. using PicView.ImageHandling;
  3. using PicView.UILogic;
  4. using PicView.UILogic.Sizing;
  5. using System;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Media.Imaging;
  11. using static PicView.ChangeImage.Error_Handling;
  12. using static PicView.ChangeImage.Navigation;
  13. using static PicView.UILogic.Tooltip;
  14. using static PicView.UILogic.TransformImage.Rotation;
  15. using static PicView.UILogic.UC;
  16. namespace PicView.FileHandling
  17. {
  18. internal static class Open_Save
  19. {
  20. internal static bool IsDialogOpen { get; set; }
  21. /// <summary>
  22. /// Files filterering string used for file/save dialog
  23. /// TODO update for and check file support
  24. /// </summary>
  25. internal const string FilterFiles =
  26. "Pictures|*.bmp;*.jpg;*.png;.tif;*.gif;*.ico;*.jpeg;*.webp;*" // Common pics
  27. + "|jpg| *.jpg;*.jpeg*" // JPG
  28. + "|PNG|*.png;" // PNG
  29. + "|gif|*.gif;" // GIF
  30. + "|ico|*.ico;" // ICO
  31. + "|svg|*.svg;" // SVG
  32. + "|webp|*.webp;" // WEBP
  33. + "|tga|*.tga;" // TGA
  34. + "|dds|*.dds;" // DDS
  35. + "|ico|*.ico;" // ICO
  36. + "|wdp|*.wdp;" // WDP
  37. + "|svg|*.svg;" // SVG
  38. + "|Photoshop|*.psd;*.psb" // PSD
  39. + "|GIMP|*.xcf" // GIMP
  40. + "|Archives|*.zip;*.7zip;*.7z;*.rar;*.bzip2;*.tar;*.wim;*.iso;*.cab" // Archives
  41. + "|Comics|*.cbr;*.cb7;*.cbt;*.cbz;*.xz" // Comics
  42. + "|Camera files|*.orf;*.cr2;*.crw;*.dng;*.raf;*.ppm;*.raw;*.mrw;*.nef;*.pef;*.3xf;*.arw"; // Camera files
  43. /// <summary>
  44. /// Opens image in File Explorer
  45. /// </summary>
  46. internal static void Open_In_Explorer()
  47. {
  48. if (Pics?.Count > 0)
  49. {
  50. if (Pics.Count < FolderIndex)
  51. {
  52. return;
  53. }
  54. }
  55. else
  56. {
  57. return;
  58. }
  59. if (!File.Exists(Pics[FolderIndex]) || ConfigureWindows.GetMainWindow.MainImage.Source == null)
  60. {
  61. return;
  62. }
  63. try
  64. {
  65. Close_UserControls();
  66. FileFunctions.OpenFolderAndSelectItem(Path.GetDirectoryName(Pics?[FolderIndex]), Pics?[FolderIndex]); // https://stackoverflow.com/a/39427395
  67. }
  68. #if DEBUG
  69. catch (InvalidCastException e)
  70. {
  71. Trace.WriteLine("Open_In_Explorer exception \n" + e.Message);
  72. }
  73. #else
  74. catch (InvalidCastException) { }
  75. #endif
  76. }
  77. /// <summary>
  78. /// Open a file dialog where user can select a supported file
  79. /// </summary>
  80. internal static async Task OpenAsync()
  81. {
  82. IsDialogOpen = true;
  83. var dlg = new OpenFileDialog()
  84. {
  85. Filter = FilterFiles,
  86. Title = $"{Application.Current.Resources["OpenFileDialog"]} - {SetTitle.AppName}"
  87. };
  88. if (dlg.ShowDialog().HasValue)
  89. {
  90. await LoadPiFromFileAsync(dlg.FileName).ConfigureAwait(false);
  91. }
  92. else
  93. {
  94. return;
  95. }
  96. await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, () =>
  97. {
  98. if (ScaleImage.XWidth < 1)
  99. {
  100. ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
  101. ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
  102. }
  103. Close_UserControls();
  104. });
  105. }
  106. /// <summary>
  107. /// Start Windows "Open With" function
  108. /// </summary>
  109. /// <param name="file">The absolute path to the file</param>
  110. internal static void OpenWith(string file)
  111. {
  112. try
  113. {
  114. using var process = new Process();
  115. process.StartInfo.FileName = "openwith";
  116. process.StartInfo.Arguments = $"\"{file}\"";
  117. process.StartInfo.ErrorDialog = true;
  118. process.Start();
  119. }
  120. catch (Exception e)
  121. {
  122. #if DEBUG
  123. Trace.WriteLine("OpenWith exception \n" + e.Message);
  124. #endif
  125. ShowTooltipMessage(e.Message, true);
  126. }
  127. }
  128. /// <summary>
  129. /// Open a File Dialog, where the user can save a supported file type.
  130. /// </summary>
  131. internal static async Task SaveFilesAsync()
  132. {
  133. if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
  134. {
  135. return;
  136. }
  137. string fileName;
  138. if (Pics?.Count > FolderIndex)
  139. {
  140. if (string.IsNullOrEmpty(Pics[FolderIndex]))
  141. {
  142. return;
  143. }
  144. fileName = Path.GetFileName(Pics[FolderIndex]);
  145. }
  146. else
  147. {
  148. fileName = Path.GetRandomFileName();
  149. }
  150. var Savedlg = new SaveFileDialog()
  151. {
  152. Filter = FilterFiles,
  153. Title = Application.Current.Resources["Save"] + $" - {SetTitle.AppName}",
  154. FileName = fileName
  155. };
  156. if (!Savedlg.ShowDialog().HasValue)
  157. {
  158. return;
  159. }
  160. IsDialogOpen = true;
  161. if (ConfigureWindows.GetMainWindow.MainImage.Effect != null)
  162. {
  163. if (!SaveImages.TrySaveImageWithEffect(Savedlg.FileName))
  164. {
  165. ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
  166. }
  167. }
  168. else if (Pics?.Count > FolderIndex)
  169. {
  170. if (!SaveImages.TrySaveImage(Rotateint, Flipped, Pics[FolderIndex], Savedlg.FileName))
  171. {
  172. ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
  173. }
  174. }
  175. else
  176. {
  177. if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
  178. {
  179. ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
  180. }
  181. if (!SaveImages.TrySaveImage(Rotateint, Flipped, ConfigureWindows.GetMainWindow.MainImage.Source as BitmapSource, Savedlg.FileName))
  182. {
  183. ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
  184. }
  185. }
  186. if (Savedlg.FileName == fileName)
  187. {
  188. //Refresh the list of pictures.
  189. await ReloadAsync().ConfigureAwait(false);
  190. }
  191. Close_UserControls();
  192. IsDialogOpen = false;
  193. }
  194. /// <summary>
  195. /// Sends the file to Windows print system
  196. /// </summary>
  197. /// <param name="path">The file path</param>
  198. internal static bool Print(string path)
  199. {
  200. if (string.IsNullOrWhiteSpace(path))
  201. {
  202. return false;
  203. }
  204. if (!File.Exists(path))
  205. {
  206. return false;
  207. }
  208. using (var p = new Process())
  209. {
  210. p.StartInfo.FileName = path;
  211. p.StartInfo.Verb = "print";
  212. p.StartInfo.UseShellExecute = true;
  213. p.Start();
  214. }
  215. return true;
  216. }
  217. }
  218. }