Migrated from SixLabors to Magick.NET
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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" />
|
<PackageReference Include="WpfAnimatedGif" Version="2.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,49 +1,58 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using SixLabors.ImageSharp;
|
using ImageMagick;
|
||||||
using SixLabors.ImageSharp.Processing;
|
|
||||||
|
|
||||||
namespace GifResizer.Models;
|
namespace GifResizer.Models;
|
||||||
|
|
||||||
public class Gif : IDisposable
|
public class Gif : IDisposable
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly string _path;
|
private readonly string _path;
|
||||||
private Image? _image;
|
private MagickImageCollection? _images;
|
||||||
|
|
||||||
public Gif(string path)
|
public Gif(string path)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(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");
|
throw new ArgumentException($"File '{path}' does not exist");
|
||||||
_path = path;
|
_path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Load()
|
public void Load()
|
||||||
{
|
{
|
||||||
_image = Image.Load(_path);
|
_images?.Dispose();
|
||||||
|
_images = new MagickImageCollection(_path);
|
||||||
|
_images.Coalesce();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Resize(int width, int height)
|
public void Resize(int width, int height)
|
||||||
{
|
{
|
||||||
if(_image == null) throw new InvalidOperationException("Image hasn't been loaded yet");
|
if (_images == null)
|
||||||
if(width <= 0 || height <= 0) throw new InvalidOperationException("Please specify a valid width or height.");
|
throw new InvalidOperationException("Image hasn't been loaded yet");
|
||||||
if(width == _image.Width && height == _image.Height) return;
|
if (width <= 0 || height <= 0)
|
||||||
_image.Mutate(x => x.Resize(width, height));
|
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)
|
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);
|
var directory = Path.GetDirectoryName(outputPath);
|
||||||
if (!string.IsNullOrEmpty(directory))
|
if (!string.IsNullOrEmpty(directory))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(directory);
|
Directory.CreateDirectory(directory);
|
||||||
}
|
}
|
||||||
_image.Save(Path.Combine(outputPath));
|
|
||||||
|
_images.Write(outputPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_image?.Dispose();
|
_images?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using SixLabors.ImageSharp;
|
using ImageMagick;
|
||||||
|
|
||||||
namespace GifResizer.Models;
|
namespace GifResizer.Models;
|
||||||
|
|
||||||
@@ -13,17 +13,16 @@ public class GifData
|
|||||||
private void Load(string path)
|
private void Load(string path)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(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");
|
throw new ArgumentException($"File '{path}' does not exist");
|
||||||
|
|
||||||
using Image image = Image.Load(path);
|
using var image = new MagickImage(path);
|
||||||
Path = path;
|
Path = path;
|
||||||
Width = image.Width;
|
Width = (int)image.Width;
|
||||||
Height = image.Height;
|
Height = (int)image.Height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Path { get; private set; }
|
public string Path { get; private set; }
|
||||||
public int Width { get; private set; }
|
public int Width { get; private set; }
|
||||||
public int Height { get; private set; }
|
public int Height { get; private set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -51,7 +51,7 @@ public sealed class MainViewModel : INotifyPropertyChanged
|
|||||||
|
|
||||||
public void ResizeGif()
|
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);
|
_gifService.ResizeGif(FilePath!, GifWidth!.Value, GifHeight!.Value);
|
||||||
CommonFormats
|
CommonFormats
|
||||||
.Where(f => f.Checked)
|
.Where(f => f.Checked)
|
||||||
|
|||||||
Reference in New Issue
Block a user