Implemented core logic
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using GifResizer.Service;
|
||||||
using GifResizer.ViewModels;
|
using GifResizer.ViewModels;
|
||||||
using GifResizer.Views;
|
using GifResizer.Views;
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ public partial class App : Application
|
|||||||
{
|
{
|
||||||
base.OnStartup(e);
|
base.OnStartup(e);
|
||||||
|
|
||||||
var viewModel = new MainViewModel();
|
var viewModel = new MainViewModel(new ResizeService());
|
||||||
var mainWindow = new MainWindow(viewModel);
|
var mainWindow = new MainWindow(viewModel);
|
||||||
|
|
||||||
mainWindow.Show();
|
mainWindow.Show();
|
||||||
|
|||||||
@@ -8,6 +8,10 @@
|
|||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,6 +1,50 @@
|
|||||||
namespace GifResizer.Models;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
|
using GifResizer.Service;
|
||||||
|
using SixLabors.ImageSharp;
|
||||||
|
using SixLabors.ImageSharp.Processing;
|
||||||
|
|
||||||
public class Gif
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,20 @@
|
|||||||
namespace GifResizer.Service;
|
using GifResizer.Models;
|
||||||
|
|
||||||
|
namespace GifResizer.Service;
|
||||||
|
|
||||||
public class ResizeService
|
public class ResizeService
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public ResizeService()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ResizeGif(string filePath, int width, int height)
|
||||||
|
{
|
||||||
|
using Gif gif = new Gif(filePath);
|
||||||
|
gif.Load();
|
||||||
|
gif.Resize(width, height);
|
||||||
|
gif.Save("D:/test-output.gif");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,20 @@
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using GifResizer.Models;
|
||||||
|
using GifResizer.Service;
|
||||||
|
|
||||||
namespace GifResizer.ViewModels;
|
namespace GifResizer.ViewModels;
|
||||||
|
|
||||||
public class MainViewModel : INotifyPropertyChanged
|
public class MainViewModel : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private readonly ResizeService _resizeService;
|
||||||
public event PropertyChangedEventHandler? PropertyChanged;
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
|
|
||||||
public MainViewModel()
|
public MainViewModel(ResizeService service)
|
||||||
{
|
{
|
||||||
|
_resizeService = service;
|
||||||
FilePath = Directory.GetCurrentDirectory();
|
FilePath = Directory.GetCurrentDirectory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,6 +24,11 @@ public class MainViewModel : INotifyPropertyChanged
|
|||||||
get;
|
get;
|
||||||
set => SetField(ref field, value);
|
set => SetField(ref field, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ResizeGif(int width, int height)
|
||||||
|
{
|
||||||
|
_resizeService.ResizeGif(FilePath, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -45,6 +45,12 @@
|
|||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Border>
|
</Border>
|
||||||
|
|
||||||
|
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
|
||||||
|
<CheckBox Content="112x112" Margin="10"/>
|
||||||
|
<CheckBox Content="500x500" Margin="10"/>
|
||||||
|
<CheckBox Content="1000x1000" Margin="10"/>
|
||||||
|
</StackPanel>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
Content="Resize"
|
Content="Resize"
|
||||||
Padding="5"
|
Padding="5"
|
||||||
|
|||||||
@@ -17,24 +17,44 @@ public partial class MainWindow : Window
|
|||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_mainViewModel = viewModel;
|
_mainViewModel = viewModel;
|
||||||
DataContext = _mainViewModel;
|
DataContext = _mainViewModel;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void BrowseButton_Click(object sender, RoutedEventArgs e)
|
private void BrowseButton_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
OpenFileDialog fileDialog = new OpenFileDialog
|
try
|
||||||
{
|
{
|
||||||
Filter = "GIF files (*.gif)|*.gif|All files (*.*)|*.*",
|
OpenFileDialog fileDialog = new OpenFileDialog
|
||||||
Title = "Select a GIF file"
|
{
|
||||||
};
|
Title = "Select a GIF file",
|
||||||
|
Filter = "GIF files (*.gif)|*.gif|All files (*.*)|*.*"
|
||||||
|
};
|
||||||
|
|
||||||
if (fileDialog.ShowDialog() == true)
|
if (fileDialog.ShowDialog() == true)
|
||||||
|
{
|
||||||
|
_mainViewModel.FilePath = fileDialog.FileName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_mainViewModel.FilePath = fileDialog.FileName;
|
DisplayErrorMessage("An error occured while selecting the GIF file: " + ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResizeButton_Click(object sender, RoutedEventArgs e)
|
private void ResizeButton_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_mainViewModel.ResizeGif(128,128);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
DisplayErrorMessage("An error occured while resizing the GIF: " + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DisplayErrorMessage(string message)
|
||||||
|
{
|
||||||
|
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user