Skip to content

Commit

Permalink
#37 improvements to image resize
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Nov 30, 2021
1 parent 02ff1f6 commit e3b05ed
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
18 changes: 7 additions & 11 deletions PicView/ImageHandling/BatchFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ internal static string Run(FileInfo sourceFile, int width, int height, int quali
if (sourceFile.DirectoryName + @"\" == outputFolder)
{
_ = 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 (ext is null)
{
Expand All @@ -115,20 +112,19 @@ internal static string Run(FileInfo sourceFile, int width, int height, int quali
}
else
{
_ = SaveImages.SaveImageAsync(0, false, null, sourceFile.FullName, destination, null, false).ConfigureAwait(false);
}

if (compress.HasValue)
{
Optimize(compress.Value, destination);
SaveImages.SaveImage(null, sourceFile.FullName, destination, null, null, quality, ext);
}
}
else
{
destination = Path.ChangeExtension(destination, ext);
_ = SaveImages.SaveImageAsync(0, false, null, sourceFile.FullName, destination, null, false).ConfigureAwait(false);
SaveImages.SaveImage(null, sourceFile.FullName, destination, null, null, quality, ext);
}

Optimize(compress.Value, 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(newSize).AppendLine(Environment.NewLine);

return sb.ToString();
}

Expand Down
43 changes: 43 additions & 0 deletions PicView/ImageHandling/SaveImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,48 @@ await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(System.Windows.Threa
catch (Exception) { return false; }
return true;
}

internal static bool SaveImage(Stream? stream, string path, string? destination, int? width, int? height, int? quality, string? ext)
{
try
{
using MagickImage magickImage = new();

if (stream is not null)
{
magickImage.Read(stream);
}
else if (path is not null)
{
magickImage.Read(path);
}
else { return false; }

if (quality is not null)
{
magickImage.Quality = quality.Value;
}

if (width is not null)
{
magickImage.Resize(width.Value, 0);
}
else if (height is not null)
{
magickImage.Resize(0, height.Value);
}

if (destination is not null)
{
magickImage.Write(ext is not null ? Path.ChangeExtension(destination, ext) : destination);
}
else
{
magickImage.Write(ext is not null ? Path.ChangeExtension(path, ext) : path);
}
}
catch (Exception) { return false; }
return true;
}
}
}
37 changes: 31 additions & 6 deletions PicView/Views/Windows/ResizeWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ void Loop(CancellationToken cancellationToken)
{
return;
}
if (running is false)
{
cancelToken.Cancel();
if (sourceFileist is not null)
{
sourceFileist.Clear();
sourceFileist = null;
}
Dispatcher.Invoke(DispatcherPriority.ContextIdle, () =>
{
ProgressBar.Value = 0;
});
return;
}
FileInfo fileInfo;
lock (sourceFileist)
{
Expand All @@ -310,12 +326,22 @@ void Loop(CancellationToken cancellationToken)
for (int x = 0; x < thumbs.Count; x++)
{
sb.Append(BatchFunctions.Run(fileInfo, thumbs[x].width, thumbs[x].height, quality, ext, thumbs[x].percentage, compress, thumbs[x].directory, true));
}
if (running is false)
{
cancelToken.Cancel();
if (sourceFileist is not null)
{
sourceFileist.Clear();
sourceFileist = null;
}
Dispatcher.Invoke(DispatcherPriority.ContextIdle, () =>
{
ProgressBar.Value = 0;
});
return;
}
if (running is false)
{
cancelToken.Cancel();
sb.Append(BatchFunctions.Run(fileInfo, thumbs[x].width, thumbs[x].height, quality, ext, thumbs[x].percentage, compress, thumbs[x].directory, true));
}
if (cancelToken.IsCancellationRequested)
Expand All @@ -327,7 +353,6 @@ void Loop(CancellationToken cancellationToken)
}
Dispatcher.Invoke(DispatcherPriority.ContextIdle, () =>
{
LogTextBox.Text = string.Empty;
ProgressBar.Value = 0;
});
return;
Expand Down

0 comments on commit e3b05ed

Please sign in to comment.