OptimizeDirectory Method

Summary

Optimizes a directory of images using the specified optimization options, and saves the optimized images to a new directory with the same original hierarchy.

Syntax
C#
VB
C++
  
Public Sub OptimizeDirectory( _ 
   ByVal codecs As RasterCodecs, _ 
   ByVal inputDirectory As String, _ 
   ByVal outputDirectory As String, _ 
   ByVal options As ImageOptimizerOptions, _ 
   ByVal fileExtensions As String, _ 
   ByVal includeSubDirectories As Boolean, _ 
   ByVal directoryCallback As ImageOptimizerDirectory _ 
)  
public: 
void OptimizeDirectory(  
   RasterCodecs^ codecs, 
   String^ inputDirectory, 
   String^ outputDirectory, 
   ImageOptimizerOptions options, 
   String^ fileExtensions, 
   bool includeSubDirectories, 
   ImageOptimizerDirectory^ directoryCallback 
)  

Parameters

codecs
The Leadtools.Codecs.RasterCodecs object used internally in the optimization operation to load an image and optimize it.

inputDirectory
A System.String that contains the full directory path to the images to be optimized.

outputDirectory
A System.String that contains the full directory path to be used when saving the optimized image(s).

options
The options used in the optimization process.

fileExtensions
A System.String that contains the extensions of the files to be optimized. For example:

  • To optimize all "gif" and "jpeg" files, fileExtensions should be set to "*.gif;*.jpeg".
  • To optimize all the supported files found in the inputDirectory directory, regardless of their extensions, fileExtensions should be set to "*.*".

includeSubDirectories
true to optimize sub-directories, false otherwise.

directoryCallback
Optional callback function that provides user information about the image(s) being optimized, such as the completion percentage for the current image being optimized, and the completion percentage for all files being optimized.

Remarks

For more information, refer to Image Optimization Using The ImageOptimizer Class.

Example

This example will optimize all Jpg files found in a specific directory, and save them into a separate folder

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageOptimization; 
 
int _imageNo; 
 
public void TestDirImageOptimizer() 
{ 
   _imageNo = 0; 
 
   // Initialize the RasterCodecs class 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // The input and output directories 
   string inputDirectory = LEAD_VARS.ImagesDir; 
   string outputDirectory = Path.Combine(LEAD_VARS.ImagesDir, "OptimizedImages"); 
 
   // Initialize a new Optimizer object 
   ImageOptimizer optimizer = new ImageOptimizer(); 
 
   // Optimization Options 
   ImageOptimizerOptions options = ImageOptimizerOptions.Default; 
 
   optimizer.OptimizeDirectory(codecs, 
      inputDirectory, 
      outputDirectory, 
      options, 
      "*.jpg", 
      false, 
      ImageOptimizerDirectory); 
 
   //shutdown the RasterCodecs class. 
} 
 
bool ImageOptimizerDirectory(ImageOptimizerDirectoryData data) 
{ 
   Console.WriteLine(string.Format("File Percent = {0}%,    Total Percent = {1}%", data.FilePercent, data.TotalPercent)); 
 
   if (_imageNo == data.TotalFolderFilesCount) 
   { 
      // Operation Done. 
      Console.WriteLine("Optimization Operation Completed Successfully"); 
      return true; 
   } 
   else if (data.Status == ImageOptimizerDirectoryStatus.PreOptimizingImage) 
   { 
      string text = string.Format("Optimizing Image {0} ?\n", data.InputFileName); 
      DialogResult result = MessageBox.Show(text, "", MessageBoxButtons.YesNoCancel); 
 
      if (result == DialogResult.Yes) 
      { 
         // Optimize the image using the default options. 
         data.Options = ImageOptimizerOptions.Default; 
         return true; 
      } 
      else if (result == DialogResult.No) 
      { 
         // Skip this image. 
         _imageNo++; 
         data.SkipImage = true; 
      } 
      else 
         // Stop the whole operation. 
         return false; 
   } 
   else if (data.FilePercent == 100) 
   { 
      _imageNo++; 
 
      // Displaying information about the optimized image. 
      string msg = string.Format("Optimizing File ( {0} of {1} ) \n" + 
         "--------------------------------\n" + 
         "Source File Name = {2}\n" + 
         "Detination File Name = {3}\n" + 
         "No of Pages = {4}\n", 
         _imageNo, data.TotalFolderFilesCount, data.InputFileName, data.OutputFileName, data.ImageInfo.TotalPages); 
 
      Console.WriteLine(msg); 
   } 
   return true; 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageOptimization 
'''Imports Leadtools.Support.Examples 
 
 
Private _imageNo As Integer 
Public Sub TestDirImageOptimizer() 
   _imageNo = 0 
 
   ' Initialize the RasterCodecs class 
   Dim codecs As RasterCodecs = New RasterCodecs() 
 
   ' The input and output directories 
   Dim inputDirectory As String = LEAD_VARS.ImagesDir 
   Dim outputDirectory As String = Path.Combine(LEAD_VARS.ImagesDir, "OptimizedImages") 
 
   ' Initialize a new Optimizer object 
   Dim optimizer As ImageOptimizer = New ImageOptimizer() 
 
   ' Optimization Options 
   Dim options As ImageOptimizerOptions = ImageOptimizerOptions.Default 
 
   optimizer.OptimizeDirectory(codecs, 
      inputDirectory, 
      outputDirectory, 
      options, 
      "*.jpg", 
      False, 
      AddressOf ImageOptimizerDirectory) 
 
   'shutdown the RasterCodecs class. 
End Sub 
 
Public Function ImageOptimizerDirectory(ByVal data As ImageOptimizerDirectoryData) As Boolean 
   Console.WriteLine(String.Format("File Percent = {0}%,    Total Percent = {1}%", data.FilePercent, data.TotalPercent)) 
 
   If (_imageNo = data.TotalFolderFilesCount) Then 
 
      ' Operation Done. 
      Console.WriteLine("Optimization Operation Completed Successfully") 
      Return True 
 
   ElseIf (data.Status = ImageOptimizerDirectoryStatus.PreOptimizingImage) Then 
      Dim text As String = String.Format("Optimizing Image {0} ?\n", data.InputFileName) 
      Dim result As DialogResult = MessageBox.Show(text, "", MessageBoxButtons.YesNoCancel) 
 
      If (result = DialogResult.Yes) Then 
         ' Optimize the image using the default options. 
         data.Options = ImageOptimizerOptions.Default 
         Return True 
      ElseIf (result = DialogResult.No) Then 
         ' Skip this image. 
         _imageNo += 1 
         data.SkipImage = True 
      Else 
         ' Stop the whole operation. 
         Return False 
      End If 
   ElseIf (data.FilePercent = 100) Then 
      _imageNo += 1 
 
      ' Displaying information about the optimized image. 
      Dim msg As String = String.Format("Optimizing File ( {0} of {1} ) \n" + 
         "--------------------------------\n" + 
         "Source File Name = {2}\n" + 
         "Detination File Name = {3}\n" + 
         "No of Pages = {4}\n", 
         _imageNo, data.TotalFolderFilesCount, data.InputFileName, data.OutputFileName, data.ImageInfo.TotalPages) 
 
      Console.WriteLine(msg) 
   End If 
   Return True 
End Function 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

Help Version 20.0.2020.3.31
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.