Leadtools.Codecs Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.17
CompactFile Method
See Also  Example
Leadtools.Codecs Namespace > RasterCodecs Class : CompactFile Method




Compacts TIFF files with default options.

Overload List

OverloadDescription
CompactFile(String,String,Int32) Compacts TIFF files with default options.  
CompactFile(String,String,Int32,Int32,Boolean,Int32,Int32,Boolean,Int32,CodecsSavePageMode,Boolean,Boolean) Compacts TIFF files with specific options.  

Example

Visual BasicCopy Code
RasterCodecs.CompactFile
      Public Sub CompactFileExample()
         RasterCodecs.Startup()
         Dim codecs As RasterCodecs = New RasterCodecs()

         Dim srcFileName As String = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\CompactFile1_Src.tif"
         Dim destFileName As String = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\CompactFile1_Dest.tif"

         ' Create a RasterImage with 4 pages containing text showing the page number
         Dim image As RasterImage = Nothing

         Dim f As Font = New Font("Arial", 36, FontStyle.Bold)
         Dim btmp As Bitmap = New Bitmap(320, 200)
         Dim rc As Rectangle = New Rectangle(0, 0, btmp.Width, btmp.Height)
         Dim sf As StringFormat = New StringFormat()
         sf.Alignment = StringAlignment.Center
         sf.LineAlignment = StringAlignment.Center

         Const pageCount As Integer = 4
         For i As Integer = 1 To pageCount
            ' Create a GDI+ bitmap with the text
            Dim text As String = "Page " & i

            Dim g As Graphics = Graphics.FromImage(btmp)
            g.FillRectangle(Brushes.White, rc)
            g.DrawString(text, f, Brushes.Black, rc, sf)
            g.Dispose()

            Dim tempImage As RasterImage = New RasterImage(btmp)

            If image Is Nothing Then
               image = tempImage
            Else
               image.AddPage(tempImage)
            End If
         Next i

         sf.Dispose()
         btmp.Dispose()

         ' Save all the pages to the file
         ' The file should have 4 pages now: 1, 2, 3, 4
         codecs.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 file
         codecs.CompactFile(srcFileName, destFileName, 0)

         ' Compact the source file again and append all the pages to the existing destination file
         Dim pagesToAdd As Integer = 0 ' 0 means all pages
         Dim srcStartPage As Integer = 1
         Dim destStartPage As Integer = 5 ' insert at the end

         codecs.CompactFile(srcFileName, destFileName, pagesToAdd, srcStartPage, False, 0, destStartPage, False, 0, CodecsSavePageMode.Insert, False, False)

         ' Clean up
         codecs.Dispose()
         RasterCodecs.Shutdown()
      End Sub
C#Copy Code
RasterCodecs.CompactFile 
      public void CompactFileExample() 
      { 
         RasterCodecs.Startup(); 
         RasterCodecs codecs = new RasterCodecs(); 
 
         string srcFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\CompactFile1_Src.tif"; 
         string destFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\CompactFile1_Dest.tif"; 
 
         // Create a RasterImage with 4 pages containing text showing the page number 
         RasterImage image = null; 
 
         Font f = new Font("Arial", 36, FontStyle.Bold); 
         Bitmap btmp = new Bitmap(320, 200); 
         Rectangle rc = new Rectangle(0, 0, btmp.Width, btmp.Height); 
         StringFormat sf = new StringFormat(); 
         sf.Alignment = StringAlignment.Center; 
         sf.LineAlignment = StringAlignment.Center; 
 
         const int pageCount = 4; 
         for(int i = 1; i <= pageCount; i++) 
         { 
            // Create a GDI+ bitmap with the text 
            string text = "Page " + i; 
 
            Graphics g = Graphics.FromImage(btmp); 
            g.FillRectangle(Brushes.White, rc); 
            g.DrawString(text, f, Brushes.Black, rc, sf); 
            g.Dispose(); 
 
            RasterImage tempImage = new RasterImage(btmp); 
 
            if(image == null) 
               image = tempImage; 
            else 
               image.AddPage(tempImage); 
         } 
 
         sf.Dispose(); 
         btmp.Dispose(); 
 
         // Save all the pages to the file 
         // The file should have 4 pages now: 1, 2, 3, 4 
         codecs.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 file 
         codecs.CompactFile(srcFileName, destFileName, 0); 
 
         // Compact the source file again and append all the pages to the existing destination file 
         int pagesToAdd = 0;  // 0 means all pages 
         int srcStartPage = 1; 
         int destStartPage = 5;  // insert at the end 
 
         codecs.CompactFile( 
            srcFileName, 
            destFileName, 
            pagesToAdd, 
            srcStartPage, 
            false, 
            0, 
            destStartPage, 
            false, 
            0, 
            CodecsSavePageMode.Insert, 
            false, 
            false); 
 
         // Clean up 
         codecs.Dispose(); 
         RasterCodecs.Shutdown(); 
      }

Remarks

This method can also be used to copy or extract one or more pages from a TIFF file and copy them without recompression to another TIFF file. Whenever you save an image containing a region as a TIFF file format, the region is also saved. Note, however, that the ability to save a region inside a TIFF file must be unlocked. This requires the Document Imaging Toolkit, Document Imaging Suite, Medical Imaging Toolkit, or Medical Imaging Suite.

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also