←Select platform

Convert(string,string,RasterImageFormat,int,int,int,CodecsImageInfo) Method

Summary
Converts an image file from one format to another, creating a new file in the new format.
Syntax
C#
Objective-C
C++/CLI
Python
public void Convert( 
   string srcFileName, 
   string destFileName, 
   RasterImageFormat format, 
   int width, 
   int height, 
   int bitsPerPixel, 
   CodecsImageInfo info 
) 
- (BOOL)convertFile:(NSString *)srcFile  
    destinationFile:(NSString *)dstFile  
             format:(LTRasterImageFormat)format  
              width:(NSInteger)width  
             height:(NSInteger)height  
       bitsPerPixel:(NSInteger)bitsPerPixel  
              error:(NSError **)error 
public: 
void Convert(  
   String^ srcFileName, 
   String^ destFileName, 
   RasterImageFormat format, 
   int width, 
   int height, 
   int bitsPerPixel, 
   CodecsImageInfo^ info 
)  

Parameters

srcFileName
A String containing the input file name.

destFileName
A String containing the output file name.

format
The output file format. For valid values, refer to Summary of All Supported File Formats.

width
New width of the output image. If this value is not 0, then the output file will be resized to the  width value. Use a value of 0 if you do not wish to resize the output file.

height
New height of the output image. If this value is not 0, then the output file will be resized to the  height value. Use a value of 0 if you do not wish to resize the output file.

bitsPerPixel
The output image pixel depth. Note that not all bits per pixel are available to all file formats.

info
A CodecsImageInfo object specifying more options for the conversion.

Remarks

If you convert to a lower bits-per-pixel format, this method optimizes the colors automatically. For example, when converting a 24-bit file (16 million colors) to an 8-bit file (256 colors) this method selects the best 256 colors to represent the 24-bit image

Note that this is a high-level method that does conversion from all possible formats to all possible formats.

Example

This example will convert create a RasterImage that contains as pages, thumbnails for all of the CMP images in a folder

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
public void ConvertExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.ThrowExceptionsOnInvalidImages = false; 
 
   string srcPath = LEAD_VARS.ImagesDir; 
   string destPath = Path.Combine(LEAD_VARS.ImagesDir, "GifThumbnails"); 
 
   if (!Directory.Exists(destPath)) 
      Directory.CreateDirectory(destPath); 
 
   const int thumbWidth = 128; 
   const int thumbHeight = 128; 
   LeadRect destRect = new LeadRect(0, 0, thumbWidth, thumbHeight); 
 
   // Get the CMP files in this folder 
   string[] files = Directory.GetFiles(srcPath, "*.cmp"); 
   foreach (string srcFileName in files) 
   { 
      // Make sure that this is a file we can load 
      CodecsImageInfo info = codecs.GetInformation(srcFileName, false); 
      if (info.Format != RasterImageFormat.Unknown) 
      { 
         // Convert to thumbnails (we want to keep the aspect ratio) 
         LeadRect rc = RasterImage.CalculatePaintModeRectangle( 
            thumbWidth, 
            thumbHeight, 
            destRect, 
            RasterPaintSizeMode.FitAlways, 
            RasterPaintAlignMode.Near, 
            RasterPaintAlignMode.Near); 
 
         // rc.Width and Height contains the image size we want with good aspect ratio 
         string name = Path.GetFileNameWithoutExtension(srcFileName); 
         string destFileName = Path.Combine(destPath, name + "_thumb.gif"); 
 
         // Delete the thumbnail if its already there 
         if (File.Exists(destFileName)) 
            File.Delete(destFileName); 
 
         codecs.Convert(srcFileName, destFileName, RasterImageFormat.Gif, rc.Width, rc.Height, 8, info); 
      } 
   } 
 
   // Clean up 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.*; 
import java.net.*; 
import java.nio.file.Paths; 
import java.util.*; 
import java.time.Instant; 
import java.time.Duration; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.codecs.RasterCodecs.FeedCallbackThunk; 
import leadtools.drawing.internal.*; 
import leadtools.imageprocessing.*; 
import leadtools.imageprocessing.color.ChangeIntensityCommand; 
import leadtools.svg.*; 
 
 
public void convertExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
   codecs.setThrowExceptionsOnInvalidImages(false); 
 
   String srcPath = LEAD_VARS_IMAGES_DIR; 
   String destPath = combine(LEAD_VARS_IMAGES_DIR, "GifThumbnails"); 
   File destDir = new File(destPath); 
   File srcDir = new File(srcPath); 
   if (!destDir.exists()) 
      destDir.createNewFile(); 
 
   final int thumbWidth = 128; 
   final int thumbHeight = 128; 
   LeadRect destRect = new LeadRect(0, 0, thumbWidth, thumbHeight); 
 
   // Get the CMP files in this folder 
   String[] filesList = srcDir.list(); 
   int i = 0; 
   for (String srcFileName : filesList) { 
      if (srcFileName.endsWith(".cmp")) { 
         // Make sure that this is a file we can load 
         CodecsImageInfo info = codecs.getInformation(srcFileName, false); 
         if (info.getFormat() != RasterImageFormat.UNKNOWN) { 
            // Convert to thumbnails (we want to keep the aspect ratio) 
            LeadRect rc = RasterImage.calculatePaintModeRectangle( 
                  thumbWidth, 
                  thumbHeight, 
                  destRect, 
                  RasterPaintSizeMode.FIT_ALWAYS, 
                  RasterPaintAlignMode.NEAR, 
                  RasterPaintAlignMode.NEAR); 
 
            // rc.Width and Height contains the image size we want with good aspect ratio 
            String name = srcFileName.substring(0, srcFileName.indexOf(".cmp")); 
            String destFileName = combine(destPath, name + "_thumb.gif"); 
 
            // Delete the thumbnail if its already there 
            File destFile = new File(destFileName); 
            if (destFile.exists()) 
               destFile.delete(); 
 
            codecs.convert(srcFileName, destFileName, RasterImageFormat.GIF, rc.getWidth(), rc.getHeight(), 8, info); 
         } 
      } 
      i++; 
   } 
   assertTrue("Failed to check every file in filesList", srcDir.list().length == i); 
   System.out.println("Checked every file in filesList"); 
 
   // Clean up 
   codecs.dispose(); 
} 
Requirements

Target Platforms

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

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