Leadtools.Codecs Namespace > RasterCodecs Class > Convert Method : Convert(String,String,RasterImageFormat,Int32,Int32,Int32,CodecsImageInfo) Method |
public void Convert( string srcFileName, string destFileName, RasterImageFormat format, int width, int height, int bitsPerPixel, CodecsImageInfo info )
'Declaration 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 _ )
'Usage Dim instance As RasterCodecs Dim srcFileName As String Dim destFileName As String Dim format As RasterImageFormat Dim width As Integer Dim height As Integer Dim bitsPerPixel As Integer Dim info As CodecsImageInfo instance.Convert(srcFileName, destFileName, format, width, height, bitsPerPixel, info)
public void Convert( string srcFileName, string destFileName, RasterImageFormat format, int width, int height, int bitsPerPixel, CodecsImageInfo info )
function Leadtools.Codecs.RasterCodecs.Convert(String,String,RasterImageFormat,Int32,Int32,Int32,CodecsImageInfo)( srcFileName , destFileName , format , width , height , bitsPerPixel , info )
public: void Convert( String^ srcFileName, String^ destFileName, RasterImageFormat format, int width, int height, int bitsPerPixel, CodecsImageInfo^ info )
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.
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.ImageProcessing Imports Leadtools.ImageProcessing.Color Imports Leadtools.Drawing Public Sub ConvertExample() Dim codecs As RasterCodecs = New RasterCodecs() codecs.ThrowExceptionsOnInvalidImages = False Dim destPath As String = Path.Combine(LEAD_VARS.ImagesDir, "GifThumbnails") If (Not Directory.Exists(destPath)) Then Directory.CreateDirectory(destPath) End If Const thumbWidth As Integer = 128 Const thumbHeight As Integer = 128 Dim destRect As LeadRect = New LeadRect(0, 0, thumbWidth, thumbHeight) ' Get the CMP files in this folder Dim 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 load Dim 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 ratio Dim name As String = Path.GetFileNameWithoutExtension(srcFileName) Dim destFileName As String = Path.Combine(destPath, name & "_thumb.gif") ' Delete the thumbnail if its already there If File.Exists(destFileName) Then File.Delete(destFileName) End If codecs.Convert(srcFileName, destFileName, RasterImageFormat.Gif, rc.Width, rc.Height, 8, info) End If Next srcFileName ' Clean up codecs.Dispose() End Sub Public NotInheritable Class LEAD_VARS Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" End Class
using Leadtools; using Leadtools.Codecs; using Leadtools.ImageProcessing; using Leadtools.ImageProcessing.Color; 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:\Users\Public\Documents\LEADTOOLS Images"; }
RasterCodecsExamples.prototype.ConvertExample = function () { Tools.SetLicense(); with (Leadtools) { with (Leadtools.Codecs) { var codecs = new RasterCodecs(); codecs.throwExceptionsOnInvalidImages = false; var srcPath = "Assets"; var destPath = "GifThumbnails"; var srcFolder; var destFolder; var destRect; return Tools.AppInstallFolder().getFolderAsync(srcPath).then(function (srcFolderX) { srcFolder = srcFolderX; return Tools.AppLocalFolder().createFolderAsync(destPath, Windows.Storage.CreationCollisionOption.replaceExisting) }) .then(function (destFolderX) { destFolder = destFolderX; var thumbWidth = 128; var thumbHeight = 128; destRect = LeadRectHelper.create(0, 0, thumbWidth, thumbHeight); // Get the CMP files in this folder return srcFolder.getFilesAsync() }) .then(function (files) { var promises = files.map(function (loadFile) { return LoadAndConvertAsync(codecs, loadFile, destRect, destPath); }); return WinJS.Promise.join(promises) }) .then(function () { // Clean up codecs.close(); }); } } } function LoadAndConvertAsync ( codecs, loadFile, destRect, destPath, info ) { var thumbWidth = 128; var thumbHeight = 128; var rc; var info; with ( Leadtools ) { with ( Leadtools.Codecs ) { // Make sure that this is a file we can load return codecs.getInformationAsync(LeadStreamFactory.create(loadFile), false, 1).then ( function ( infoX ) { info = infoX; if (info == null || info.format == RasterImageFormat.unknown) return ; // Convert to thumbnails (we want to keep the aspect ratio) 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 var name = GetFileNameWithoutExtenstion( loadFile.path); var destFileName = destPath + "\\" + name + "_thumb.gif"; return Tools.AppLocalFolder().createFileAsync(destFileName, Windows.Storage.CreationCollisionOption.replaceExisting)}) .then ( function ( saveFile ) { return codecs.convertAsync(LeadStreamFactory.create(loadFile), LeadStreamFactory.create(saveFile), RasterImageFormat.gif, rc.width, rc.height, 8, info)}); } } } function GetFileNameWithoutExtenstion(path) { var fileName = path + ""; var start = fileName.lastIndexOf ( "\\") ; var end = fileName.lastIndexOf ( ".") ; return fileName.substring(start+1, end); }
using Leadtools; using Leadtools.Codecs; using Leadtools.ImageProcessing; using Leadtools.ImageProcessing.Color; public async Task ConvertExample() { RasterCodecs codecs = new RasterCodecs(); codecs.ThrowExceptionsOnInvalidImages = false; string srcPath = @"Assets"; string destPath = @"GifThumbnails"; IStorageFolder srcFolder = await Tools.AppInstallFolder.GetFolderAsync(srcPath); IStorageFolder destFolder = await Tools.AppLocalFolder.CreateFolderAsync(destPath, CreationCollisionOption.ReplaceExisting); const int thumbWidth = 128; const int thumbHeight = 128; LeadRect destRect = LeadRectHelper.Create(0, 0, thumbWidth, thumbHeight); // Get the CMP files in this folder IReadOnlyList<StorageFile> files = await srcFolder.GetFilesAsync(); foreach (StorageFile loadFile in files) { // Make sure that this is a file we can load CodecsImageInfo info = await codecs.GetInformationAsync(LeadStreamFactory.Create(loadFile), false, 1); if (info != null && 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(loadFile.Path); string destFileName = Path.Combine(destPath, name + "_thumb.gif"); StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(destFileName, CreationCollisionOption.ReplaceExisting); await codecs.ConvertAsync(LeadStreamFactory.Create(loadFile), LeadStreamFactory.Create(saveFile), RasterImageFormat.Gif, rc.Width, rc.Height, 8, info); } } // Clean up codecs.Dispose(); }
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 load CodecsImageInfo 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); }
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.ImageProcessing Imports Leadtools.ImageProcessing.Color Imports Leadtools.Windows.Media Public Sub ConvertExample(ByVal inStreamCmp As Stream, ByVal outStreamGifThumb As Stream) Dim codecs As RasterCodecs = New RasterCodecs() Const thumbWidth As Integer = 128 Const thumbHeight As Integer = 128 Dim destRect As LeadRect = New LeadRect(0, 0, thumbWidth, thumbHeight) ' Make sure that this is a file we can load Dim 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