Converts an image file from one format to another, creating a new file in the new format.
public void Convert(string srcFileName,string destFileName,RasterImageFormat format,int width,int height,int bitsPerPixel,CodecsImageInfo info)
Public Overloads Sub Convert( _ByVal srcFileName As String, _ByVal destFileName As String, _ByVal format As RasterImageFormat, _ByVal width As Integer, _ByVal height As Integer, _ByVal bitsPerPixel As Integer, _ByVal info As CodecsImageInfo _)
- (BOOL)convertFile:(NSString *)srcFiledestinationFile:(NSString *)dstFileformat:(LTRasterImageFormat)formatwidth:(NSInteger)widthheight:(NSInteger)heightbitsPerPixel:(NSInteger)bitsPerPixelerror:(NSError **)error
public:void Convert(String^ srcFileName,String^ destFileName,RasterImageFormat format,int width,int height,int bitsPerPixel,CodecsImageInfo^ info)
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.
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.
This example will convert create a RasterImage that contains as pages, thumbnails for all of the CMP images in a folder
using Leadtools;using Leadtools.Codecs;using Leadtools.ImageProcessing;using Leadtools.ImageProcessing.Color;using Leadtools.Svg;using LeadtoolsExamples.Common;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 folderstring[] files = Directory.GetFiles(srcPath, "*.cmp");foreach (string srcFileName in files){// Make sure that this is a file we can loadCodecsImageInfo 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 ratiostring name = Path.GetFileNameWithoutExtension(srcFileName);string destFileName = Path.Combine(destPath, name + "_thumb.gif");// Delete the thumbnail if its already thereif (File.Exists(destFileName))File.Delete(destFileName);codecs.Convert(srcFileName, destFileName, RasterImageFormat.Gif, rc.Width, rc.Height, 8, info);}}// Clean upcodecs.Dispose();}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.ColorImports Leadtools.DrawingImports Leadtools.SvgPublic Sub ConvertExample()Dim codecs As RasterCodecs = New RasterCodecs()codecs.ThrowExceptionsOnInvalidImages = FalseDim destPath As String = Path.Combine(LEAD_VARS.ImagesDir, "GifThumbnails")If (Not Directory.Exists(destPath)) ThenDirectory.CreateDirectory(destPath)End IfConst thumbWidth As Integer = 128Const thumbHeight As Integer = 128Dim destRect As LeadRect = New LeadRect(0, 0, thumbWidth, thumbHeight)' Get the CMP files in this folderDim files As String() = Directory.GetFiles(LEAD_VARS.ImagesDir, "*.cmp")For Each srcFileName As String In files' Make sure that this is a file we can loadDim info As CodecsImageInfo = codecs.GetInformation(srcFileName, False)If info.Format <> RasterImageFormat.Unknown Then' Convert to thumbnails (we want to keep the aspect ratio)Dim rc As LeadRect = RasterImage.CalculatePaintModeRectangle(thumbWidth, thumbHeight, destRect, RasterPaintSizeMode.FitAlways, RasterPaintAlignMode.Near, RasterPaintAlignMode.Near)' rc.Width and Height contains the image size we want with good aspect ratioDim name As String = Path.GetFileNameWithoutExtension(srcFileName)Dim destFileName As String = Path.Combine(destPath, name & "_thumb.gif")' Delete the thumbnail if its already thereIf File.Exists(destFileName) ThenFile.Delete(destFileName)End Ifcodecs.Convert(srcFileName, destFileName, RasterImageFormat.Gif, rc.Width, rc.Height, 8, info)End IfNext srcFileName' Clean upcodecs.Dispose()End SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
c#[Silverlight C# Example]using Leadtools;using Leadtools.Codecs;using Leadtools.Examples;using Leadtools.ImageProcessing;using Leadtools.ImageProcessing.Color;using Leadtools.Windows.Media;public void ConvertExample(Stream inStreamCmp, Stream outStreamGifThumb){RasterCodecs codecs = new RasterCodecs();const int thumbWidth = 128;const int thumbHeight = 128;LeadRect destRect = new LeadRect(0, 0, thumbWidth, thumbHeight);// Make sure that this is a file we can loadCodecsImageInfo info = codecs.GetInformation(inStreamCmp, false);// Convert to thumbnails (we want to keep the aspect ratio)LeadRect rc = RasterImage.CalculatePaintModeRectangle(thumbWidth,thumbHeight,destRect,RasterPaintSizeMode.FitAlways,RasterPaintAlignMode.Near,RasterPaintAlignMode.Near);codecs.Convert(inStreamCmp, outStreamGifThumb, RasterImageFormat.Gif, rc.Width, rc.Height, 8, info);}vb[Silverlight VB Example]Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.ColorImports Leadtools.Windows.MediaPublic Sub ConvertExample(ByVal inStreamCmp As Stream, ByVal outStreamGifThumb As Stream)Dim codecs As RasterCodecs = New RasterCodecs()Const thumbWidth As Integer = 128Const thumbHeight As Integer = 128Dim destRect As LeadRect = New LeadRect(0, 0, thumbWidth, thumbHeight)' Make sure that this is a file we can loadDim info As CodecsImageInfo = codecs.GetInformation(inStreamCmp, False)' Convert to thumbnails (we want to keep the aspect ratio)Dim rc As LeadRect = RasterImage.CalculatePaintModeRectangle(thumbWidth, thumbHeight, destRect, RasterPaintSizeMode.FitAlways, RasterPaintAlignMode.Near, RasterPaintAlignMode.Near)codecs.Convert(inStreamCmp, outStreamGifThumb, RasterImageFormat.Gif, rc.Width, rc.Height, 8, info)End Sub
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
