Leadtools.Codecs Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
CompactFile(String,String,Int32) Method
See Also  Example
Leadtools.Codecs Namespace > RasterCodecs Class > CompactFile Method : CompactFile(String,String,Int32) Method



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.
Compacts TIFF files with default options.

Syntax

Visual Basic (Declaration) 
Public Overloads Sub CompactFile( _
   ByVal srcFileName As String, _
   ByVal destFileName As String, _
   ByVal pages As Integer _
) 
Visual Basic (Usage)Copy Code
Dim instance As RasterCodecs
Dim srcFileName As String
Dim destFileName As String
Dim pages As Integer
 
instance.CompactFile(srcFileName, destFileName, pages)
C# 
public void CompactFile( 
   string srcFileName,
   string destFileName,
   int pages
)
C++/CLI 
public:
void CompactFile( 
   String^ srcFileName,
   String^ destFileName,
   int pages
) 

Parameters

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.

Example

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

   Dim srcFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "CompactFile1_Src.tif"
   Dim destFileName As String = LeadtoolsExamples.Common.ImagesPath.Path + "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
public void CompactFileExample() 

   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "CompactFile1_Src.tif"; 
   string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "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 a Document Imaging or Medical Imaging toolkit.

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also