Migrated from SixLabors to Magick.NET

This commit is contained in:
Laurent
2026-02-17 19:38:16 +01:00
parent 423c74dcb8
commit 5132ec1c16
4 changed files with 29 additions and 21 deletions

View File

@@ -11,7 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="14.10.2" />
<PackageReference Include="WpfAnimatedGif" Version="2.0.2" />
</ItemGroup>

View File

@@ -1,49 +1,58 @@
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using ImageMagick;
namespace GifResizer.Models;
public class Gif : IDisposable
{
private readonly string _path;
private Image? _image;
private MagickImageCollection? _images;
public Gif(string path)
{
ArgumentNullException.ThrowIfNull(path);
if (path.Length == 0 || path.IsWhiteSpace() || !File.Exists(path))
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
throw new ArgumentException($"File '{path}' does not exist");
_path = path;
}
public void Load()
{
_image = Image.Load(_path);
_images?.Dispose();
_images = new MagickImageCollection(_path);
_images.Coalesce();
}
public void Resize(int width, int height)
{
if(_image == null) throw new InvalidOperationException("Image hasn't been loaded yet");
if(width <= 0 || height <= 0) throw new InvalidOperationException("Please specify a valid width or height.");
if(width == _image.Width && height == _image.Height) return;
_image.Mutate(x => x.Resize(width, height));
if (_images == null)
throw new InvalidOperationException("Image hasn't been loaded yet");
if (width <= 0 || height <= 0)
throw new InvalidOperationException("Please specify a valid width or height.");
foreach (var image in _images)
{
if (image.Width == width && image.Height == height) continue;
image.Resize((uint)width, (uint)height);
}
}
public void Save(string outputPath)
{
if(_image == null) throw new InvalidOperationException("Image hasn't been loaded yet");
if (_images == null)
throw new InvalidOperationException("Image hasn't been loaded yet");
var directory = Path.GetDirectoryName(outputPath);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}
_image.Save(Path.Combine(outputPath));
_images.Write(outputPath);
}
public void Dispose()
{
_image?.Dispose();
_images?.Dispose();
}
}

View File

@@ -1,5 +1,5 @@
using System.IO;
using SixLabors.ImageSharp;
using ImageMagick;
namespace GifResizer.Models;
@@ -13,17 +13,16 @@ public class GifData
private void Load(string path)
{
ArgumentNullException.ThrowIfNull(path);
if (path.Length == 0 || path.IsWhiteSpace() || !File.Exists(path))
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
throw new ArgumentException($"File '{path}' does not exist");
using Image image = Image.Load(path);
using var image = new MagickImage(path);
Path = path;
Width = image.Width;
Height = image.Height;
Width = (int)image.Width;
Height = (int)image.Height;
}
public string Path { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
}

View File

@@ -51,7 +51,7 @@ public sealed class MainViewModel : INotifyPropertyChanged
public void ResizeGif()
{
if(FilePath == null && GifWidth == null && GifHeight == null) return;
if(FilePath == null && GifWidth == null && GifHeight == null) throw new InvalidOperationException("Please select a file before trying to resize.");
_gifService.ResizeGif(FilePath!, GifWidth!.Value, GifHeight!.Value);
CommonFormats
.Where(f => f.Checked)