Skip to content

Instantly share code, notes, and snippets.

@exts
Created April 14, 2019 03:28
Show Gist options
  • Save exts/6ebc6278e19d880ccc6617f624b3b436 to your computer and use it in GitHub Desktop.
Save exts/6ebc6278e19d880ccc6617f624b3b436 to your computer and use it in GitHub Desktop.
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