Compacts TIFF/BigTIFF files with default options.
public void CompactFile(string srcFileName,string destFileName,int pages)
Public Overloads Sub CompactFile( _ByVal srcFileName As String, _ByVal destFileName As String, _ByVal pages As Integer _)
- (BOOL)compactFile:(NSString *)sourceFiledestinationFile:(NSString *)destinationFilepages:(NSUInteger)pageserror:(NSError **)error
public:void CompactFile(String^ srcFileName,String^ destFileName,int pages)
srcFileName
A String containing the name of the file being compacted. All the pages will be read from this file.
destFileName
A String containing the name of the file in which all the pages will be written.
pages
Value that represents the number of pages to copy. Use 0 to copy all the pages. If pages is > 0, only pages will be copied to destFileName.
This method can also be used to copy or extract one or more pages from a TIFF/BigTIFF file and copy them without recompression to another TIFF/BigTIFF file.
This method can also be used to convert a TIFF file to BigTIFF or vice versa. The destination file will be Tiff or BigTiff depending on BigTiff.
using Leadtools;using Leadtools.Codecs;using Leadtools.ImageProcessing;using Leadtools.ImageProcessing.Color;using Leadtools.Svg;using LeadtoolsExamples.Common;public void CompactFileExample(){RasterCodecs codecs = new RasterCodecs();string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Src.tif");string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Dest.tif");// Create a RasterImage with 4 pages containing text showing the page numberRasterImage image = null;const int pageCount = 4;using (System.Drawing.Font f = new System.Drawing.Font("Arial", 36, System.Drawing.FontStyle.Bold))using (System.Drawing.Bitmap btmp = new System.Drawing.Bitmap(320, 200))using (System.Drawing.StringFormat sf = new System.Drawing.StringFormat()){System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, btmp.Width, btmp.Height);sf.Alignment = System.Drawing.StringAlignment.Center;sf.LineAlignment = System.Drawing.StringAlignment.Center;for (int i = 1; i <= pageCount; i++){// Create a GDI+ bitmap with the textstring text = "Page " + i;using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(btmp)){g.FillRectangle(System.Drawing.Brushes.White, rc);g.DrawString(text, f, System.Drawing.Brushes.Black, rc, sf);}RasterImage tempImage = Leadtools.Drawing.RasterImageConverter.ConvertFromImage(btmp, Leadtools.Drawing.ConvertFromImageOptions.None);if (image == null)image = tempImage;elseimage.AddPage(tempImage);}}// Save all the pages to the file// The file should have 4 pages now: 1, 2, 3, 4codecs.Save(image, srcFileName, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite);image.Dispose();// All the pages in the source TIFF file. This will create the destination filecodecs.CompactFile(srcFileName, destFileName, 0);// Compact the source file again and append all the pages to the existing destination fileint pagesToAdd = 0; // 0 means all pagesint srcStartPage = 1;int destStartPage = 5; // insert at the endcodecs.CompactFile(srcFileName,destFileName,pagesToAdd,srcStartPage,false,0,destStartPage,false,0,CodecsSavePageMode.Insert,false,false);// 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 CompactFileExample()Dim codecs As RasterCodecs = New RasterCodecs()Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Src.tif")Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "CompactFile1_Dest.tif")' Create a RasterImage with 4 pages containing text showing the page numberDim image As RasterImage = NothingConst pageCount As Integer = 4Using f As New System.Drawing.Font("Arial", 36, System.Drawing.FontStyle.Bold)Using btmp As New System.Drawing.Bitmap(320, 200)Using sf As New System.Drawing.StringFormat()Dim rc As New System.Drawing.Rectangle(0, 0, btmp.Width, btmp.Height)sf.Alignment = System.Drawing.StringAlignment.Centersf.LineAlignment = System.Drawing.StringAlignment.CenterFor i As Integer = 1 To pageCount' Create a GDI+ bitmap with the textDim text As String = "Page " + i.ToString()Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(btmp)g.FillRectangle(System.Drawing.Brushes.White, rc)g.DrawString(text, f, System.Drawing.Brushes.Black, rc, sf)End UsingDim tempImage As RasterImage = RasterImageConverter.ConvertFromImage(btmp, ConvertFromImageOptions.None)If image Is Nothing Thenimage = tempImageElseimage.AddPage(tempImage)End IfNextEnd UsingEnd UsingEnd Using' Save all the pages to the file' The file should have 4 pages now: 1, 2, 3, 4codecs.Save(image, srcFileName, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite)image.Dispose()' All the pages in the source TIFF file. This will create the destination filecodecs.CompactFile(srcFileName, destFileName, 0)' Compact the source file again and append all the pages to the existing destination fileDim pagesToAdd As Integer = 0 ' 0 means all pagesDim srcStartPage As Integer = 1Dim destStartPage As Integer = 5 ' insert at the endcodecs.CompactFile(srcFileName, destFileName, pagesToAdd, srcStartPage, False, 0, destStartPage, False, 0, CodecsSavePageMode.Insert, False, False)' 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 CompactFileExample(Stream dstStream, int width, int height, int pageCount){// Create a RasterImage with number of pages containing text showing the page numberRasterImage image = null;// The white background canvasCanvas canvas = new Canvas();canvas.Width = width;canvas.Height = height;canvas.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));// The black text blockTextBlock textBlock = new TextBlock();textBlock.FontFamily = new FontFamily("Arial");textBlock.FontSize = 36;textBlock.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));canvas.Children.Add(textBlock);for (int i = 1; i <= pageCount; i++){// The text we want on the pagestring text = "Page " + i;textBlock.Text = text;// Center the textdouble left = (canvas.Width - textBlock.ActualWidth) / 2;double top = (canvas.Height - textBlock.ActualHeight) / 2;textBlock.SetValue(Canvas.LeftProperty, left);textBlock.SetValue(Canvas.TopProperty, top);// Create a Writeable bitmap with the textWriteableBitmap wb = new WriteableBitmap(320, 200);wb.Render(canvas, null);wb.Invalidate();// Convert it to a RasterImageRasterImage pageImage = RasterImageConverter.ConvertFromSource(wb, ConvertFromSourceOptions.None);// Add it as a page to our main raster imageif (image == null)image = pageImage;elseimage.AddPage(pageImage);}RasterCodecs codecs = new RasterCodecs();// Save all the pages to the file// The file should have 4 pages now: 1, 2, 3, 4codecs.Save(image, dstStream, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite);image.Dispose();MemoryStream memoryStream = new MemoryStream((int)dstStream.Length);// All the pages in the source TIFF file. This will create the destination filecodecs.CompactFile(dstStream, memoryStream, 0);// Compact the source file again and append all the pages to the existing destination fileint pagesToAdd = 0; // 0 means all pagesint srcStartPage = 1;int destStartPage = 5; // insert at the endcodecs.CompactFile(dstStream,memoryStream,pagesToAdd,srcStartPage,false,0,destStartPage,false,0,CodecsSavePageMode.Insert,false,false);}vb[Silverlight VB Example]Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.ColorImports Leadtools.Windows.MediaPublic Sub CompactFileExample(ByVal dstStream As Stream, ByVal width As Integer, ByVal height As Integer, ByVal pageCount As Integer)' Create a RasterImage with number of pages containing text showing the page numberDim image As RasterImage = Nothing' The white background canvasDim canvas As Canvas = New Canvas()canvas.Width = widthcanvas.Height = heightcanvas.Background = New SolidColorBrush(Color.FromArgb(255, 255, 255, 255))' The black text blockDim textBlock As TextBlock = New TextBlock()textBlock.FontFamily = New FontFamily("Arial")textBlock.FontSize = 36textBlock.Foreground = New SolidColorBrush(Color.FromArgb(255, 0, 0, 0))canvas.Children.Add(textBlock)Dim i As Integer = 1Do While i <= pageCount' The text we want on the pageDim text As String = "Page " & itextBlock.Text = text' Center the textDim left As Double = (canvas.Width - textBlock.ActualWidth) / 2Dim top As Double = (canvas.Height - textBlock.ActualHeight) / 2textBlock.SetValue(canvas.LeftProperty, left)textBlock.SetValue(canvas.TopProperty, top)' Create a Writeable bitmap with the textDim wb As WriteableBitmap = New WriteableBitmap(320, 200)wb.Render(canvas, Nothing)wb.Invalidate()' Convert it to a RasterImageDim pageImage As RasterImage = RasterImageConverter.ConvertFromSource(wb, ConvertFromSourceOptions.None)' Add it as a page to our main raster imageIf image Is Nothing Thenimage = pageImageElseimage.AddPage(pageImage)End Ifi += 1LoopDim codecs As RasterCodecs = New RasterCodecs()' Save all the pages to the file' The file should have 4 pages now: 1, 2, 3, 4codecs.Save(image, dstStream, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite)image.Dispose()Dim memoryStream As MemoryStream = New MemoryStream(CInt(dstStream.Length))' All the pages in the source TIFF file. This will create the destination filecodecs.CompactFile(dstStream, memoryStream, 0)' Compact the source file again and append all the pages to the existing destination fileDim pagesToAdd As Integer = 0 ' 0 means all pagesDim srcStartPage As Integer = 1Dim destStartPage As Integer = 5 ' insert at the endcodecs.CompactFile(dstStream, memoryStream, pagesToAdd, srcStartPage, False, 0, destStartPage, False, 0, CodecsSavePageMode.Insert, False, False)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
