Creating Multipage TIFF Images

TX Text Control .NET 15.0 introduced the page rendering engine that allows you to export a metafile or a bitmap of each separate page. This enables developers to create thumbnails of the pages or to export images to view them in a browser. This sample shows how to create a multipage TIFF image from all pages of a document.

Two significant steps are required to create these images:

First, it is required to iterate through all pages of TX Text Control to create separate TIFF images:

ArrayList inputImages = new ArrayList();

foreach (Page page in textControl1.GetPages()) 
  MemoryStream image = new MemoryStream(); 
  Bitmap mf = page.GetImage(100, TXTextControl.Page.PageContent.All); 
  mf.Save(image, ImageFormat.Tiff); 
[Visual Basic]
Dim inputImages As New ArrayList()

For Each page As Page In textControl1.GetPages()
  Dim image As New MemoryStream()
  Dim mf As Bitmap = page.GetImage(100, TXTextControl.Page.PageContent.All)
  mf.Save(image, ImageFormat.Tiff)

Each TIFF image is stored in a memory stream which is added to an ArrayList for an easier handling when combining them.

In a second step, the TIFF images are combined to a single image. Therefore, a new image is created in order to append all other images from the ArrayList to a new frame of the new image using the SaveAdd method.

public static void CreateMultipageTIF(ArrayList InputImages, string Filename)
 // set the image codec
 ImageCodecInfo info = null;
 foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
   if (ice.MimeType == "image/tiff")
     info = ice;

 EncoderParameters ep = new EncoderParameters(2);

 bool firstPage = true;

 System.Drawing.Image img = null;

 // create a image instance from the 1st image
 for (int nLoopfile = 0; nLoopfile < InputImages.Count; nLoopfile++)
   //get image from src file
   System.Drawing.Image img_src = System.Drawing.Image.FromStream((Stream)InputImages[nLoopfile]);

   Guid guid = img_src.FrameDimensionsList[0];
   System.Drawing.Imaging.FrameDimension dimension = new System.Drawing.Imaging.FrameDimension(guid);

   //get the frames from src file
   for (int nLoopFrame = 0; nLoopFrame < img_src.GetFrameCount(dimension); nLoopFrame++)
     img_src.SelectActiveFrame(dimension, nLoopFrame);

     ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Convert.ToInt32(EncoderValue.CompressionLZW));

     // if first page, then create the initial image
     if (firstPage)
       img = img_src;

       ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, Convert.ToInt32(EncoderValue.MultiFrame));
       img.Save(Filename, info, ep);

       firstPage = false;

     // add image to the next frame
     ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, Convert.ToInt32(EncoderValue.FrameDimensionPage));
     img.SaveAdd(img_src, ep);

 ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, Convert.ToInt32(EncoderValue.Flush));
[Visual Basic]
Public Shared Sub CreateMultipageTIF(InputImages As ArrayList, Filename As String)
  ' set the image codec
  Dim info As ImageCodecInfo = Nothing
  For Each ice As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
    If ice.MimeType = "image/tiff" Then
      info = ice
      Exit For
    End If

  Dim ep As New EncoderParameters(2)

  Dim firstPage As Boolean = True

  Dim img As System.Drawing.Image = Nothing

 ' create a image instance from the 1st image
 For nLoopfile As Integer = 0 To InputImages.Count - 1
   ' get image from src file
   Dim img_src As System.Drawing.Image = System.Drawing.Image.FromStream(DirectCast(InputImages(nLoopfile), Stream))

   Dim guid As Guid = img_src.FrameDimensionsList(0)
   Dim dimension As New System.Drawing.Imaging.FrameDimension(guid)

   ' get the frames from src file
   For nLoopFrame As Integer = 0 To img_src.GetFrameCount(dimension) - 1
     img_src.SelectActiveFrame(dimension, nLoopFrame)

     ep.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Convert.ToInt32(EncoderValue.CompressionLZW))

     ' if first page, then create the initial image
     If firstPage Then
       img = img_src

       ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, Convert.ToInt32(EncoderValue.MultiFrame))
       img.Save(Filename, info, ep)

       firstPage = False
       Continue For
     End If

     ' add image to the next frame
      ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, Convert.ToInt32(EncoderValue.FrameDimensionPage))
     img.SaveAdd(img_src, ep)

 ep.Param(1) = New EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, Convert.ToInt32(EncoderValue.Flush))
End Sub

<< Back