←Select platform

ImageOptimizer Class

Summary
Provides methods to optimize one image or a directory of images in one of the supported formats.
Syntax
C#
C++/CLI
Python
public class ImageOptimizer 
public ref class ImageOptimizer  
class ImageOptimizer: 
Remarks

If the image is a 16-bit, 24-bit, or 32-bit image, the real number of colors used in the image is calculated and when possible the image is saved with lower bits per pixel. If the image is 1-bit, 4-bit, or 8-bits per pixel, the Percent and Distance are used. For more information, refer to Image Optimization Using The ImageOptimizer Class.

Example

This example will optimize a PNG image file and then save it to a separate folder

C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageOptimization; 
 
 
public void TestPngImageOptimizer() 
{ 
   // Initialize the RasterCodecs class 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // The input and output location 
   string inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "LittleGFlyingAlpha.png"); 
   string outputFolder = Path.Combine(LEAD_VARS.ImagesDir, "OptimizedImages"); 
 
   // Initialize a new Optimizer object 
   ImageOptimizer optimizer = new ImageOptimizer(); 
 
   // Optimization Options 
   ImageOptimizerOptions options = ImageOptimizerOptions.Default; 
 
   // Set custom optimization options 
   options.Distance = 20; 
   options.Percent = 15; 
   options.PngQualityFactor = 4; 
 
   IntPtr bufferPtr; 
   int bufferSize = 0; 
   LoadFileIntoPointer(inputFileName, out bufferPtr, out bufferSize); 
 
   if (IntPtr.Zero != bufferPtr && bufferSize > 0) 
   { 
      RasterNativeBuffer optBuffer = optimizer.OptimizeBuffer(codecs, bufferPtr, bufferSize, options, null); 
 
      // Free orgBuffer.PointerBuffer, since it won't be needed anymore. 
      Marshal.FreeHGlobal(bufferPtr); 
 
      // Save this image into the output folder 
      // Make sure the output folder exists 
      if (!Directory.Exists(outputFolder)) 
         Directory.CreateDirectory(outputFolder); 
 
      // Get the name of the output file from the input file 
      string outputFileName = Path.Combine(outputFolder, Path.GetFileName(inputFileName)); 
 
      // Save the optimized buffer to the output file 
      using (FileStream fs = File.Create(outputFileName)) 
      { 
         byte[] optArray = new byte[optBuffer.Length]; 
 
         Marshal.Copy(optBuffer.Data, optArray, 0, optArray.Length); 
         fs.Write(optArray, 0, optArray.Length); 
      } 
 
      // Free optBuffer.PointerBuffer, since it won't be needed anymore. 
      Marshal.FreeHGlobal(optBuffer.Data); 
 
      // Compare the original image size with the optimized size. 
      long orgSize = new FileInfo(inputFileName).Length; 
      long optSize = new FileInfo(outputFileName).Length; 
      int percentage = (int)((double)optSize * 100.0 / orgSize); 
 
      string message = string.Format( 
         "Original image size: {0} KB{1}Optimized image size: {2} KB{1}Percentage: {3}%", 
         orgSize / 1024, Environment.NewLine, optSize / 1024, 
         100 - percentage); 
      MessageBox.Show(message); 
   } 
 
   //shutdown the RasterCodecs class. 
} 
 
// This method opens an image file and loads it into IntPtr. 
private void LoadFileIntoPointer(string fileName, out IntPtr ptr, out int size) 
{ 
   using (FileStream fs = File.OpenRead(fileName)) 
   { 
      // Allocate memory to load the file 
      size = (int)fs.Length; 
      ptr = Marshal.AllocHGlobal(size); 
 
      // Load in 32K chunks 
      const int bufferSize = 32 * 1024; 
      byte[] buffer = new byte[bufferSize]; 
 
      int bytesToRead; 
      int bytesLeft = (int)fs.Length; 
      IntPtr tempPtr = ptr;   // where we are 
      do 
      { 
         // read a chunk 
         bytesToRead = Math.Min(bufferSize, bytesLeft); 
         if (bytesToRead > 0) 
         { 
            fs.Read(buffer, 0, bytesToRead); 
 
            // copy into our buffer 
            Marshal.Copy(buffer, 0, tempPtr, bytesToRead); 
 
            // move the temp pointer 
            tempPtr = new IntPtr(tempPtr.ToInt64() + bytesToRead); 
 
            bytesLeft -= bytesToRead; 
         } 
      } 
      while (bytesToRead > 0); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

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

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.