SHARE
TWEET

Untitled

a guest Apr 24th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Drawing;
  5. using ImageProcessor;
  6. using ImageProcessor.Imaging.Formats;
  7.  
  8. namespace CardResizer
  9. {
  10.     internal class Program
  11.     {
  12.         private const string DirectoryPath = "cardpack";
  13.         private const string OutputDirectoryPath = "output";
  14.        
  15.         public static void Main(string[] args)
  16.         {
  17.             var resizeSize = new Size(456, 638);
  18.             var outputFormat = new PngFormat();
  19.            
  20.             var files = Directory.GetFiles(DirectoryPath);
  21.             foreach(var file in files.ToList())
  22.             {
  23.                 var filename = GetFilename(file, DirectoryPath);
  24.                 var outputFilename = GetFullFilename(filename, OutputDirectoryPath);
  25.                
  26.                 // cleanup
  27.                 DeleteIfExists(outputFilename);
  28.                
  29.                 var data = File.ReadAllBytes(file);
  30.                 if(data.Length <= 0)
  31.                 {
  32.                     throw new Exception($"There was a problem attempt to read byte data from image: {filename}");
  33.                 }
  34.  
  35.                 using(var inputStream = new MemoryStream(data))
  36.                 {
  37.                     using(var image = new ImageFactory(true))
  38.                     {
  39.                         image.Load(inputStream)
  40.                             .Resize(resizeSize)
  41.                             .Format(outputFormat)
  42.                             .Save(outputFilename);
  43.                     }
  44.                 }
  45.                
  46.             }
  47.         }
  48.  
  49.         private static string GetFilename(string filename, string directory)
  50.         {
  51.             return filename.Replace(directory + "\\", "").ToLower();
  52.         }
  53.  
  54.         private static string GetFullFilename(string filename, string directory)
  55.         {
  56.             return $"{directory}/{filename}";
  57.         }
  58.  
  59.         private static void DeleteIfExists(string filepath)
  60.         {
  61.             if(File.Exists(filepath))
  62.             {
  63.                 File.Delete(filepath);    
  64.             }
  65.         }
  66.     }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top