浏览代码

#37 optimize batch conversion

Ruben 3 年之前
父节点
当前提交
e6f71f4e66

+ 34 - 34
PicView/ImageHandling/BatchFunctions.cs

@@ -2,9 +2,7 @@
 using PicView.FileHandling;
 using PicView.UILogic;
 using System;
-using System.Collections.Generic;
 using System.IO;
-using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Controls;
@@ -14,7 +12,7 @@ namespace PicView.ImageHandling
 {
     internal static class BatchFunctions
     {
-        internal static async Task RunAsync(List<string> sourceFileist,
+        internal static void Run(FileInfo sourceFile,
                                             int resizeAmount,
                                             int quality,
                                             string? ext,
@@ -23,49 +21,51 @@ namespace PicView.ImageHandling
                                             string outputFolder,
                                             bool toResize,
                                             TextBox LogTextBox,
-                                            ProgressBar progressBar) => await Task.Run(() =>
+                                            ProgressBar progressBar)
         {
-            Parallel.For(0, sourceFileist.Count, async i =>
+            string? destination = outputFolder is null ? null : outputFolder + @"/" + sourceFile.Name;
+
+            var sb = new StringBuilder();
+
+            if (Directory.Exists(outputFolder) == false)
             {
-                var sourceFile = new FileInfo(sourceFileist[i]);
-                var destination = outputFolder + sourceFile.Name;
-                var sb = new StringBuilder();
+                Directory.CreateDirectory(outputFolder);
+            }
 
-                if (toResize)
+            if (toResize)
+            {
+                _ = doResize(LogTextBox, progressBar, sb, sourceFile, resizeAmount, quality, percentage, destination, compress, ext).ConfigureAwait(false);
+            }
+            else if (compress.HasValue)
+            {
+                if (sourceFile.DirectoryName == outputFolder)
                 {
-                    _ = doResize(LogTextBox, progressBar, sb, sourceFile, resizeAmount, quality, percentage, destination, compress, ext).ConfigureAwait(false);
+                    _ = ImageFunctions.OptimizeImageAsync(sourceFile.FullName).ConfigureAwait(false);
+                    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(newSize).AppendLine(Environment.NewLine);
                 }
-                else if (compress.HasValue)
+                else if (ext is null)
                 {
-                    if (sourceFile.DirectoryName == outputFolder)
+                    if (quality is 100)
                     {
-                        await ImageFunctions.OptimizeImageAsync(sourceFile.FullName).ConfigureAwait(false);
-                        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(newSize).AppendLine(Environment.NewLine);
+                        File.Copy(sourceFile.FullName, destination, true);
+                        _ = ImageFunctions.OptimizeImageAsync(destination).ConfigureAwait(false);
                     }
                     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);
+                        _ = SaveImages.SaveImageAsync(0, false, null, sourceFile.FullName, destination, null, false).ConfigureAwait(false);
                     }
-
-
-                    report(LogTextBox, progressBar, sb);
                 }
-            });
-        });
+                else
+                {
+                    destination = Path.ChangeExtension(destination, ext);
+                    _ = SaveImages.SaveImageAsync(0, false, null, sourceFile.FullName, destination, null, false).ConfigureAwait(false);
+                }
+
+                report(LogTextBox, progressBar, sb);
+            }
+        }
 
         static async Task doResize(TextBox LogTextBox,
                                    ProgressBar progressBar,

+ 3 - 2
PicView/Views/UserControls/Misc/ThumbnailOutputUC.xaml.cs

@@ -14,16 +14,17 @@ namespace PicView.Views.UserControls
             OutPutStringBox.Text = folderPath + @"\" + filename;
             ValueBox.Text = value;
 
-            OutputFolderButton.FileMenuButton.Click += (_, _) => 
+            OutputFolderButton.FileMenuButton.Click += (_, _) =>
             {
                 var newFolder = FileHandling.Open_Save.SelectAndReturnFolder();
                 if (string.IsNullOrWhiteSpace(newFolder) == false)
                 {
                     OutPutStringBox.Text = newFolder;
                 }
+                UILogic.ConfigureWindows.GetResizeWindow.Focus();
             };
 
             Windows.ResizeWindow.SetTextboxDragEvent(OutPutStringBox);
-         }
+        }
     }
 }

+ 1 - 1
PicView/Views/Windows/ImageInfoWindow.xaml

@@ -158,7 +158,7 @@
 
             <Button
                 x:Name="FileProperties"
-                Width="128"
+                Width="125"
                 Height="30"
                 Padding="0"
                 Style="{StaticResource MetroFlatButton}"

+ 17 - 7
PicView/Views/Windows/ResizeWindow.xaml.cs

@@ -57,6 +57,7 @@ namespace PicView.Views.Windows
                     {
                         SourceFolderInput.Text = newFolder;
                     }
+                    Focus();
                 };
 
                 OutputFolderButton.FileMenuButton.Click += (_, _) =>
@@ -66,6 +67,7 @@ namespace PicView.Views.Windows
                     {
                         OutputFolderInput.Text = newFolder;
                     }
+                    Focus();
                 };
 
                 ThumbnailsComboBox.SelectionChanged += delegate
@@ -239,16 +241,24 @@ namespace PicView.Views.Windows
 
                     ProgressBar.Maximum = thumbs.Count > 0 ? sourceFileist.Count * thumbs.Count : sourceFileist.Count;
 
-                    await BatchFunctions.RunAsync(sourceFileist, resizeAmount, quality, ext, percentage, compress, outputFolder, toResize, LogTextBox, ProgressBar).ConfigureAwait(false);
-
-                    Parallel.For(0, thumbs.Count, async i =>
+                    await Task.Run(() =>
                     {
-                        var thumbLoc = thumbs[i].directory + @"\" + Path.GetFileName(sourceFileist[i]);
-                        if (string.IsNullOrWhiteSpace(thumbLoc) == false)
+                        Parallel.For(0, sourceFileist.Count, i =>
                         {
-                            await BatchFunctions.RunAsync(sourceFileist, thumbs[i].size, quality, ext, thumbs[i].percentage, compress, thumbLoc, true, LogTextBox, ProgressBar).ConfigureAwait(false);
-                        }
+                            FileInfo fileInfo;
+                            lock (sourceFileist)
+                            {
+                                fileInfo = new FileInfo(sourceFileist[i]);
+                            }
+
+                            BatchFunctions.Run(fileInfo, resizeAmount, quality, ext, percentage, compress, outputFolder, toResize, LogTextBox, ProgressBar);
+                            for (int x = 0; x < thumbs.Count; x++)
+                            {
+                                BatchFunctions.Run(fileInfo, thumbs[x].size, quality, ext, thumbs[x].percentage, compress, thumbs[x].directory, true, LogTextBox, ProgressBar);
+                            }
+                        });
                     });
+
                 };
 
                 CancelButton.MouseEnter += delegate { MouseOverAnimations.ButtonMouseOverAnim(CancelText); };