Resize an Entire Folder of Images

This morning I was given a folder of more than 100 screenshots of our various demos. I needed these to update our website. Unfortunately, most of them were too large for the page and needed to be resized. I quickly created a .NET Core console app to fix the image sizes for me. I was able to write the code, resize the images, and then blog about it before 10:00am. Code is below. Enjoy!

using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using System;
using System.IO;

namespace BatchResize_NetCore
{
   internal class Program
   {
      private static void Main(string[] args)
      {
         if (!SetLicense())
            return;
         // TODO: Change these as needed
         const string filter = @"*.png";
         const string sourceFolder = @"D:∖temp∖readme screenshots∖";

         var directoryInfo = new DirectoryInfo(sourceFolder);
         foreach (var file in directoryInfo.GetFiles(filter))
         {
            ShowMessage($"Processing {file.Name}");
            ResizeImage(file.FullName);
         }
         ShowMessage("Processing complete.");
      }

      public static void ResizeImage(string sourcePath, string destinationPath = null)
      {
         const float max = 500f; // The max width or height

         if (destinationPath == null) destinationPath = sourcePath;
         LeadSize CalculateNewSize(RasterImage i)
         {
            var ratio = i.Width > i.Height ? max / i.Width : max / i.Height;
            return new LeadSize((int)(i.Width * ratio), (int)(i.Height * ratio));
         }
         using (var codecs = new RasterCodecs())
         using (var image = codecs.Load(sourcePath))
         {
            // 9 is slower but results in small file size
            // https://www.leadtools.com/help/leadtools/v20/dh/co/codecspngsaveoptions-qualityfactor.html
            codecs.Options.Png.Save.QualityFactor = 9; 
            if (image.Width <= 500 && image.Height <= max)
               return; // does not need to be resized
            var newSize = CalculateNewSize(image);
            new SizeCommand
            {
               Width = newSize.Width,
               Height = newSize.Height,
               Flags = RasterSizeFlags.Bicubic | RasterSizeFlags.ScaleToGray
            }.Run(image);
            codecs.Save(image, destinationPath, RasterImageFormat.Png, image.BitsPerPixel);
         }
      }

      public static bool SetLicense()
      {
         try
         {
            // TODO: Replace these with the path to the LEADTOOLS license file
            const string licenseFilePath = null;
            var developerKey = null;

            if (developerKey != null)
               RasterSupport.SetLicense(licenseFilePath, developerKey);
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex.Message);
         }

         return !RasterSupport.KernelExpired;
      }

      public static void ShowMessage(string message, bool error = false)
      {
         var origColor = Console.ForegroundColor;
         Console.ForegroundColor = error ? ConsoleColor.Red : ConsoleColor.Green;
         Console.WriteLine(message);
         Console.ForegroundColor = origColor;
      }
   }
}

About 

Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in Image Processing. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *