Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement