Converts an image file from one format to another, creating a new file in the new format.
Syntax
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 Image 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 obect specifying more options for the conversion.
Example
This example will convert create a RasterImage that contains as pages, thumbnails for all the CMP images in a folder
Visual Basic | Copy Code |
---|
Public Sub ConvertExample()
RasterCodecs.Startup()
Dim codecs As RasterCodecs = New RasterCodecs()
codecs.ThrowExceptionsOnInvalidImages = False
Dim srcPath As String = LeadtoolsExamples.Common.ImagesPath.Path
Dim destPath As String = Path.Combine(srcPath, "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 Rectangle = New Rectangle(0, 0, thumbWidth, thumbHeight)
Dim files As String() = Directory.GetFiles(srcPath, "*.cmp")
For Each srcFileName As String In files
Dim info As CodecsImageInfo = codecs.GetInformation(srcFileName, False)
If info.Format <> RasterImageFormat.Unknown Then
Dim rc As Rectangle = RasterImage.CalculatePaintModeRectangle(thumbWidth, thumbHeight, destRect, RasterPaintSizeMode.FitAlways, RasterPaintAlignMode.Near, RasterPaintAlignMode.Near)
Dim name As String = Path.GetFileNameWithoutExtension(srcFileName)
Dim destFileName As String = Path.Combine(destPath, name & "_thumb.gif")
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
codecs.Dispose()
RasterCodecs.Shutdown()
End Sub |
C# | Copy Code |
---|
public void ConvertExample() { RasterCodecs.Startup(); RasterCodecs codecs = new RasterCodecs(); codecs.ThrowExceptionsOnInvalidImages = false; string srcPath = LeadtoolsExamples.Common.ImagesPath.Path; string destPath = Path.Combine(srcPath, @"GifThumbnails"); if(!Directory.Exists(destPath)) Directory.CreateDirectory(destPath); const int thumbWidth = 128; const int thumbHeight = 128; Rectangle destRect = new Rectangle(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) Rectangle 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(); RasterCodecs.Shutdown(); } |
Remarks
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family
See Also