Files
gif-resizer/GifResizer/Models/Gif.cs
2026-02-17 00:05:24 +01:00

50 lines
1.3 KiB
C#

using System.Diagnostics;
using System.IO;
using GifResizer.Service;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
namespace GifResizer.Models;
public class Gif : IDisposable
{
private readonly FileInfo _file;
private Image? _image;
public Gif(string path)
{
ArgumentNullException.ThrowIfNull(path);
if (path.Length == 0 || path.IsWhiteSpace() || !File.Exists(path))
throw new ArgumentException($"File '{path}' does not exist");
_file = new FileInfo(path);
}
public void Load()
{
_image = Image.Load(_file.FullName);
}
public void Resize(int width, int height)
{
if(_image == null) throw new InvalidOperationException("Image hasn't been loaded yet");
_image.Mutate(x => x.Resize(width, height));
}
public void Save(string outputPath)
{
if(_image == 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));
}
public void Dispose()
{
_image?.Dispose();
}
}