Просмотр исходного кода

#37 Add conversion, work in progress

Ruben 4 лет назад
Родитель
Сommit
4ae297e832

+ 38 - 9
PicView/ImageHandling/BatchFunctions.cs

@@ -14,9 +14,16 @@ namespace PicView.ImageHandling
 {
     internal static class BatchFunctions
     {
-        internal static async Task RunAsync(List<string> sourceFileist, int resizeAmount, int quality,
-            Percentage? percentage, bool? compress, string outputFolder, bool toResize, TextBox LogTextBox,
-            ProgressBar progressBar) => await Task.Run(() =>
+        internal static async Task RunAsync(List<string> sourceFileist,
+                                            int resizeAmount,
+                                            int quality,
+                                            string? ext,
+                                            Percentage? percentage,
+                                            bool? compress,
+                                            string outputFolder,
+                                            bool toResize,
+                                            TextBox LogTextBox,
+                                            ProgressBar progressBar) => await Task.Run(() =>
         {
             Parallel.For(0, sourceFileist.Count, async i =>
             {
@@ -26,30 +33,52 @@ namespace PicView.ImageHandling
 
                 if (toResize)
                 {
-                    _ = doResize(LogTextBox, progressBar, sb, sourceFile, resizeAmount, quality, percentage, destination, compress).ConfigureAwait(false);
+                    _ = doResize(LogTextBox, progressBar, sb, sourceFile, resizeAmount, quality, percentage, destination, compress, ext).ConfigureAwait(false);
                 }
                 else if (compress.HasValue)
                 {
                     if (sourceFile.DirectoryName == outputFolder)
                     {
                         await ImageFunctions.OptimizeImageAsync(sourceFile.FullName).ConfigureAwait(false);
-                        var destinationFile = new FileInfo(destination);
+                        var newSize = FileFunctions.GetSizeReadable(new FileInfo(sourceFile.FullName).Length);
                         sb.Append(sourceFile.DirectoryName).Append('/').Append(sourceFile.Name).Append(' ').Append(FileFunctions.GetSizeReadable(sourceFile.Length))
-                            .Append(" 🠚 ").Append(sourceFile.Name).Append(' ').Append(FileFunctions.GetSizeReadable(sourceFile.Length)).AppendLine(Environment.NewLine);
+                            .Append(" 🠚 ").Append(sourceFile.Name).Append(' ').Append(newSize).AppendLine(Environment.NewLine);
                     }
                     else
                     {
-
+                        if (Directory.Exists(outputFolder) == false)
+                        {
+                            Directory.CreateDirectory(outputFolder);
+                        }
+                        if (quality is 100)
+                        {
+                            File.Copy(sourceFile.FullName, destination, true);
+                        }
+                        else
+                        {
+                            // Change quality
+                        }
+                        await ImageFunctions.OptimizeImageAsync(destination).ConfigureAwait(false);
                     }
 
+
                     report(LogTextBox, progressBar, sb);
                 }
             });
         });
 
-        static async Task doResize(TextBox LogTextBox, ProgressBar progressBar, StringBuilder sb, FileInfo? sourceFile, int resizeAmount, int quality = 100, Percentage? percentage = null, string? destination = null, bool? compress = null)
+        static async Task doResize(TextBox LogTextBox,
+                                   ProgressBar progressBar,
+                                   StringBuilder sb,
+                                   FileInfo? sourceFile,
+                                   int resizeAmount,
+                                   int quality = 100,
+                                   Percentage? percentage = null,
+                                   string? destination = null,
+                                   bool? compress = null,
+                                   string? ext = null)
         {
-            var success = await ImageSizeFunctions.ResizeImageAsync(sourceFile.FullName, resizeAmount, resizeAmount, quality, percentage, destination, compress).ConfigureAwait(false);
+            var success = await ImageSizeFunctions.ResizeImageAsync(sourceFile.FullName, resizeAmount, resizeAmount, quality, percentage, destination, compress, ext).ConfigureAwait(false);
             if (success is false) { return; }
 
             var destinationFile = new FileInfo(destination);

+ 17 - 2
PicView/ImageHandling/ImageSizeFunctions.cs

@@ -41,7 +41,7 @@ namespace PicView.ImageHandling
             return await GetImageSizeAsync(fileInfo).ConfigureAwait(false);
         }
 
-        internal static async Task<bool> ResizeImageAsync(string file, int width, int height, int quality = 100, Percentage? percentage = null, string? destination = null, bool? compress = null)
+        internal static async Task<bool> ResizeImageAsync(string file, int width, int height, int quality = 100, Percentage? percentage = null, string? destination = null, bool? compress = null, string? ext = null)
         {
             if (string.IsNullOrWhiteSpace(file)) { return false; }
             if (File.Exists(file) == false) { return false; }
@@ -82,6 +82,10 @@ namespace PicView.ImageHandling
 
                 if (destination is null)
                 {
+                    if (ext is not null)
+                    {
+                        Path.ChangeExtension(file, ext);
+                    }
                     await magick.WriteAsync(file).ConfigureAwait(false);
                 }
                 else
@@ -92,6 +96,10 @@ namespace PicView.ImageHandling
                     {
                         Directory.CreateDirectory(dir);
                     }
+                    if (ext is not null)
+                    {
+                        Path.ChangeExtension(destination, ext);
+                    }
                     await magick.WriteAsync(destination).ConfigureAwait(false);
                 }
             }
@@ -118,7 +126,14 @@ namespace PicView.ImageHandling
                 {
                     return true;
                 }
-                imageOptimizer.Compress(x);
+                try
+                {
+                    imageOptimizer.Compress(x);
+                }
+                catch (System.Exception)
+                {
+                    return true;
+                }
             }
 
             return true;

+ 20 - 2
PicView/Views/Windows/ResizeWindow.xaml.cs

@@ -166,6 +166,24 @@ namespace PicView.Views.Windows
                         quality = q;
                     }
 
+                    string? ext;
+                    if (webp.IsSelected)
+                    {
+                        ext = ".webp";
+                    }
+                    else if (png.IsSelected)
+                    {
+                        ext = ".png";
+                    }
+                    else if (jpg.IsSelected)
+                    {
+                        ext = ".jpg";
+                    }
+                    else
+                    {
+                        ext = null;
+                    }
+
                     if (toResize)
                     {
                         if (PercentageResize.IsSelected && int.TryParse(PercentageBox.Text, out var number))
@@ -221,14 +239,14 @@ namespace PicView.Views.Windows
 
                     ProgressBar.Maximum = thumbs.Count > 0 ? sourceFileist.Count * thumbs.Count : sourceFileist.Count;
 
-                    await BatchFunctions.RunAsync(sourceFileist, resizeAmount, quality, percentage, compress, outputFolder, toResize, LogTextBox, ProgressBar).ConfigureAwait(false);
+                    await BatchFunctions.RunAsync(sourceFileist, resizeAmount, quality, ext, percentage, compress, outputFolder, toResize, LogTextBox, ProgressBar).ConfigureAwait(false);
 
                     Parallel.For(0, thumbs.Count, async i =>
                     {
                         var thumbLoc = thumbs[i].directory + @"\" + Path.GetFileName(sourceFileist[i]);
                         if (string.IsNullOrWhiteSpace(thumbLoc) == false)
                         {
-                            await BatchFunctions.RunAsync(sourceFileist, thumbs[i].size, quality, thumbs[i].percentage, compress, thumbLoc, true, LogTextBox, ProgressBar).ConfigureAwait(false);
+                            await BatchFunctions.RunAsync(sourceFileist, thumbs[i].size, quality, ext, thumbs[i].percentage, compress, thumbLoc, true, LogTextBox, ProgressBar).ConfigureAwait(false);
                         }
                     });
                 };