Skip to content

Instantly share code, notes, and snippets.

@exts

exts/imageResizer.cs

Created Apr 14, 2019
Embed
What would you like to do?
Nice little image resizer that's using nuget package ImageProcessor
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using ImageProcessor;
using ImageProcessor.Imaging.Formats;
namespace CardResizer
{
internal class Program
{
private const string DirectoryPath = "cardpack";
private const string OutputDirectoryPath = "output";
public static void Main(string[] args)
{
var resizeSize = new Size(456, 638);
var outputFormat = new PngFormat();
var files = Directory.GetFiles(DirectoryPath);
foreach(var file in files.ToList())
{
var filename = GetFilename(file, DirectoryPath);
var outputFilename = GetFullFilename(filename, OutputDirectoryPath);
// cleanup
DeleteIfExists(outputFilename);
var data = File.ReadAllBytes(file);
if(data.Length <= 0)
{
throw new Exception($"There was a problem attempt to read byte data from image: {filename}");
}
using(var inputStream = new MemoryStream(data))
{
using(var image = new ImageFactory(true))
{
image.Load(inputStream)
.Resize(resizeSize)
.Format(outputFormat)
.Save(outputFilename);
}
}
}
}
private static string GetFilename(string filename, string directory)
{
return filename.Replace(directory + "\\", "").ToLower();
}
private static string GetFullFilename(string filename, string directory)
{
return $"{directory}/{filename}";
}
private static void DeleteIfExists(string filepath)
{
if(File.Exists(filepath))
{
File.Delete(filepath);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment