Skip to content

Commit

Permalink
Batch resize - Implement adjusting width and height, setting aspect r…
Browse files Browse the repository at this point in the history
…atio #165
  • Loading branch information
Ruben2776 committed Dec 6, 2024
1 parent 5fee57b commit 6aec6d9
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions src/PicView.Avalonia/Views/BatchResizeView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace PicView.Avalonia.Views;

public partial class BatchResizeView : UserControl
{
private bool _isKeepingAspectRatio;
private bool _isKeepingAspectRatio = true;

public BatchResizeView()
{
Expand Down Expand Up @@ -58,14 +58,14 @@ public BatchResizeView()
if (_isKeepingAspectRatio)
{
_isKeepingAspectRatio = false;
LinkChainImage.IsVisible = true;
UnlinkChainImage.IsVisible = false;
LinkChainImage.IsVisible = false;
UnlinkChainImage.IsVisible = true;
}
else
{
_isKeepingAspectRatio = true;
LinkChainImage.IsVisible = false;
UnlinkChainImage.IsVisible = true;
LinkChainImage.IsVisible = true;
UnlinkChainImage.IsVisible = false;
}
};

Expand All @@ -88,30 +88,30 @@ private async Task StartBatchResize()
}

var files = await Task.FromResult(FileListHelper.RetrieveFiles(new FileInfo(SourceFolderTextBox.Text)));

if (!Directory.Exists(OutputFolderTextBox.Text))
{
Directory.CreateDirectory(OutputFolderTextBox.Text);
}

var outputFolder = string.IsNullOrWhiteSpace(OutputFolderTextBox.Text)
? SourceFolderTextBox.Text
: OutputFolderTextBox.Text;

var toConvert = !NoConversion.IsSelected;
var pngSelected = PngItem.IsSelected;
var jpgSelected = JpgItem.IsSelected;
var webpSelected = WebpItem.IsSelected;
var avifSelected = AvifItem.IsSelected;
var heicSelected = HeicItem.IsSelected;
var jxlSelected = JxlItem.IsSelected;

var qualityEnabled = IsQualityEnabledBox.IsChecked.HasValue && IsQualityEnabledBox.IsChecked.Value;
var qualityValue = (uint)QualitySlider.Value;

var losslessCompress = Lossless.IsSelected;
var lossyCompress = Lossy.IsSelected;

Percentage? percentage = null;
if (PercentageResizeBox.IsSelected)
{
Expand All @@ -120,15 +120,43 @@ private async Task StartBatchResize()
percentage = new Percentage(percentageValue);
}
}


uint width = 0, height = 0;
if (WidthResizeBox.IsSelected)
{
if (uint.TryParse(WidthValueBox.Text, out var widthValue))
{
width = widthValue;
}
}
else if (HeightResizeBox.IsSelected)
{
if (uint.TryParse(HeightValueBox.Text, out var heightValue))
{
height = heightValue;
}
}
else if (WidthAndHeightResizeBox.IsSelected)
{
if (uint.TryParse(WidthAndHeightWidthValueBox.Text, out var widthValue))
{
width = widthValue;
}

if (uint.TryParse(WidthAndHeightHeightValueBox.Text, out var heightValue))
{
height = heightValue;
}
}

ProgressBar.Maximum = files.Count();

await Parallel.ForEachAsync(files, async (file, token) =>
{
var ext = Path.GetExtension(file);

var destination = Path.Combine(outputFolder, Path.GetFileName(file));
uint width = 0, height = 0;

if (toConvert)
{
if (pngSelected)
Expand Down Expand Up @@ -166,13 +194,16 @@ await Parallel.ForEachAsync(files, async (file, token) =>
uint? quality = null;
if (qualityEnabled)
{
if (ext.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(destination).Equals(".jpg", StringComparison.OrdinalIgnoreCase) ||
if (ext.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(destination)
.Equals(".jpg", StringComparison.OrdinalIgnoreCase) ||
Path.GetExtension(destination).Equals(".jpeg", StringComparison.OrdinalIgnoreCase) ||
ext.Equals(".png", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(destination).Equals(".png", StringComparison.OrdinalIgnoreCase))
ext.Equals(".png", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(destination)
.Equals(".png", StringComparison.OrdinalIgnoreCase))
{
quality = qualityValue;
}
}

var success = await SaveImageFileHelper.SaveImageAsync(null,
file,
destination,
Expand All @@ -189,12 +220,9 @@ await Parallel.ForEachAsync(files, async (file, token) =>
if (success)
{
#if DEBUG
Console.WriteLine($"Saved {file} to {destination}");
Console.WriteLine($"Saved {file} to {destination}");
#endif
await Dispatcher.UIThread.InvokeAsync(() =>
{
ProgressBar.Value++;
});
await Dispatcher.UIThread.InvokeAsync(() => { ProgressBar.Value++; });
}
});
}
Expand Down

0 comments on commit 6aec6d9

Please sign in to comment.