How-To: 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:

  • Create a TIFF image using the page rendering engine
  • Combine those images to a single TIFF image

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 bitmap = page.GetImage(100, TXTextControl.Page.PageContent.All);
    bitmap.Save(image, ImageFormat.Tiff);
Dim inputImages As New ArrayList()

For Each page As Page In textControl1.GetPages()
    Dim image As New MemoryStream()
    Dim bitmap As Bitmap = page.GetImage(100, TXTextControl.Page.PageContent.All)
    bitmap.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 an 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));
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 an 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