| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- using Microsoft.Win32;
- using PicView.ImageHandling;
- using PicView.UILogic;
- using PicView.UILogic.Sizing;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using static PicView.ChangeImage.Error_Handling;
- using static PicView.ChangeImage.Navigation;
- using static PicView.UILogic.Tooltip;
- using static PicView.UILogic.TransformImage.Rotation;
- using static PicView.UILogic.UC;
- namespace PicView.FileHandling
- {
- internal static class Open_Save
- {
- internal static bool IsDialogOpen { get; set; }
- /// <summary>
- /// Files filterering string used for file/save dialog
- /// TODO update for and check file support
- /// </summary>
- internal const string FilterFiles =
- "Pictures|*.bmp;*.jpg;*.png;.tif;*.gif;*.ico;*.jpeg;*.webp;*" // Common pics
- + "|jpg| *.jpg;*.jpeg*" // JPG
- + "|PNG|*.png;" // PNG
- + "|gif|*.gif;" // GIF
- + "|ico|*.ico;" // ICO
- + "|svg|*.svg;" // SVG
- + "|webp|*.webp;" // WEBP
- + "|tga|*.tga;" // TGA
- + "|dds|*.dds;" // DDS
- + "|ico|*.ico;" // ICO
- + "|wdp|*.wdp;" // WDP
- + "|svg|*.svg;" // SVG
- + "|Photoshop|*.psd;*.psb" // PSD
- + "|GIMP|*.xcf" // GIMP
- + "|Archives|*.zip;*.7zip;*.7z;*.rar;*.bzip2;*.tar;*.wim;*.iso;*.cab" // Archives
- + "|Comics|*.cbr;*.cb7;*.cbt;*.cbz;*.xz" // Comics
- + "|Camera files|*.orf;*.cr2;*.crw;*.dng;*.raf;*.ppm;*.raw;*.mrw;*.nef;*.pef;*.3xf;*.arw"; // Camera files
- /// <summary>
- /// Opens image in File Explorer
- /// </summary>
- internal static void Open_In_Explorer()
- {
- if (Pics?.Count > 0)
- {
- if (Pics.Count < FolderIndex)
- {
- return;
- }
- }
- else
- {
- return;
- }
- if (!File.Exists(Pics[FolderIndex]) || ConfigureWindows.GetMainWindow.MainImage.Source == null)
- {
- return;
- }
- try
- {
- Close_UserControls();
- FileFunctions.OpenFolderAndSelectItem(Path.GetDirectoryName(Pics?[FolderIndex]), Pics?[FolderIndex]); // https://stackoverflow.com/a/39427395
- }
- #if DEBUG
- catch (InvalidCastException e)
- {
- Trace.WriteLine("Open_In_Explorer exception \n" + e.Message);
- }
- #else
- catch (InvalidCastException) { }
- #endif
- }
- /// <summary>
- /// Open a file dialog where user can select a supported file
- /// </summary>
- internal static async Task OpenAsync()
- {
- IsDialogOpen = true;
- var dlg = new OpenFileDialog()
- {
- Filter = FilterFiles,
- Title = $"{Application.Current.Resources["OpenFileDialog"]} - {SetTitle.AppName}"
- };
- if (dlg.ShowDialog().HasValue)
- {
- await LoadPiFromFileAsync(dlg.FileName).ConfigureAwait(false);
- }
- else
- {
- return;
- }
- await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, () =>
- {
- if (ScaleImage.XWidth < 1)
- {
- ConfigureWindows.GetMainWindow.MainImage.Width = ConfigureWindows.GetMainWindow.ParentContainer.ActualWidth;
- ConfigureWindows.GetMainWindow.MainImage.Height = ConfigureWindows.GetMainWindow.ParentContainer.ActualHeight;
- }
- Close_UserControls();
- });
- }
- /// <summary>
- /// Start Windows "Open With" function
- /// </summary>
- /// <param name="file">The absolute path to the file</param>
- internal static void OpenWith(string file)
- {
- try
- {
- using var process = new Process();
- process.StartInfo.FileName = "openwith";
- process.StartInfo.Arguments = $"\"{file}\"";
- process.StartInfo.ErrorDialog = true;
- process.Start();
- }
- catch (Exception e)
- {
- #if DEBUG
- Trace.WriteLine("OpenWith exception \n" + e.Message);
- #endif
- ShowTooltipMessage(e.Message, true);
- }
- }
- /// <summary>
- /// Open a File Dialog, where the user can save a supported file type.
- /// </summary>
- internal static async Task SaveFilesAsync()
- {
- if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
- {
- return;
- }
- string fileName;
- if (Pics?.Count > FolderIndex)
- {
- if (string.IsNullOrEmpty(Pics[FolderIndex]))
- {
- return;
- }
- fileName = Path.GetFileName(Pics[FolderIndex]);
- }
- else
- {
- fileName = Path.GetRandomFileName();
- }
- var Savedlg = new SaveFileDialog()
- {
- Filter = FilterFiles,
- Title = Application.Current.Resources["Save"] + $" - {SetTitle.AppName}",
- FileName = fileName
- };
- if (!Savedlg.ShowDialog().HasValue)
- {
- return;
- }
- IsDialogOpen = true;
- if (ConfigureWindows.GetMainWindow.MainImage.Effect != null)
- {
- if (!SaveImages.TrySaveImageWithEffect(Savedlg.FileName))
- {
- ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
- }
- }
- else if (Pics?.Count > FolderIndex)
- {
- if (!SaveImages.TrySaveImage(Rotateint, Flipped, Pics[FolderIndex], Savedlg.FileName))
- {
- ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
- }
- }
- else
- {
- if (ConfigureWindows.GetMainWindow.MainImage.Source == null)
- {
- ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
- }
- if (!SaveImages.TrySaveImage(Rotateint, Flipped, ConfigureWindows.GetMainWindow.MainImage.Source as BitmapSource, Savedlg.FileName))
- {
- ShowTooltipMessage(Application.Current.Resources["SavingFileFailed"]);
- }
- }
- if (Savedlg.FileName == fileName)
- {
- //Refresh the list of pictures.
- await ReloadAsync().ConfigureAwait(false);
- }
- Close_UserControls();
- IsDialogOpen = false;
- }
- /// <summary>
- /// Sends the file to Windows print system
- /// </summary>
- /// <param name="path">The file path</param>
- internal static bool Print(string path)
- {
- if (string.IsNullOrWhiteSpace(path))
- {
- return false;
- }
- if (!File.Exists(path))
- {
- return false;
- }
- using (var p = new Process())
- {
- p.StartInfo.FileName = path;
- p.StartInfo.Verb = "print";
- p.StartInfo.UseShellExecute = true;
- p.Start();
- }
- return true;
- }
- }
- }
|