Reference Guide
Programming / TIFF / Extracting pages from a multipage TIFF file using a memory stream
In This Topic
    Extracting pages from a multipage TIFF file using a memory stream
    In This Topic

    This handy code snippet can be useful when you need to extract more pages from your multi-page source TIFF file to a multi-page destination TIFF file. This example uses a memory stream to extract required pages. It shows you how to handle properly with multi-page TIFF files.

    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    Dim oGdPictureImaging As GdPictureImaging = New GdPictureImaging()
    'Variable to hold the source image handle (source file).
    Dim sourceImageId As Integer = 0
    'Variable to hold the destination image handle (destination file).
    Dim destImageId As Integer = 0
    'Variable to hold the imageID for the extracted page.
    Dim imageId As Integer = 0
    'The source file path and the destination file path.
    Dim source As String = "source.tif", destination As String = "destination.tif"
    'Loading the source image file.
    sourceImageId = oGdPictureImaging.CreateGdPictureImageFromFile(source)
    If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then
        Dim isMultiPage As Boolean = oGdPictureImaging.TiffIsMultiPage(sourceImageId)
        Dim status As GdPictureStatus = oGdPictureImaging.GetStat()
        If (status = GdPictureStatus.OK) AndAlso isMultiPage Then
            Dim pagesCount As Integer = oGdPictureImaging.TiffGetPageCount(sourceImageId)
            'Setting the page numbers you want to extract.
            Dim pages As Integer() = {1, 3, 5}
            'Ensuring that the multi-page tiff source file includes the required pages.
            If pagesCount >= 5 Then
                Dim firstPage As Boolean = True
                For Each page As Integer In pages
                    'Temporary stream for the extracted page.
                    Dim pStream As Stream = New MemoryStream()
                    status = oGdPictureImaging.TiffExtractPage(sourceImageId, page, pStream)
                    If status = GdPictureStatus.OK Then
                        imageId = oGdPictureImaging.CreateGdPictureImageFromStream(pStream)
                        status = oGdPictureImaging.GetStat()
                        If status = GdPictureStatus.OK Then
                            If firstPage Then
                                'If it is the first image, then create a new multipage tiff file.
                                destImageId = imageId
                                status = oGdPictureImaging.TiffSaveAsMultiPageFile(destImageId, destination, TiffCompression.TiffCompressionAUTO)
                                If status <> GdPictureStatus.OK Then Exit For
                                firstPage = False
                            Else
                                'If it is the second image or more, add it to the previously created multipage tiff file.
                                status = oGdPictureImaging.TiffAddToMultiPageFile(destImageId, imageId)
                                If status <> GdPictureStatus.OK Then Exit For
                                'Releasing the current image to minimize the memory usage.
                                oGdPictureImaging.ReleaseGdPictureImage(imageId)
                            End If
                        Else
                            Exit For
                        End If
                    Else
                        Exit For
                    End If
                    pStream.Dispose()
                Next
                If status = GdPictureStatus.OK Then
                    MessageBox.Show("Done!", "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBox.Show("Error: " + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MessageBox.Show("Not enough pages to extract.", "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("Either the source file is not a multi-page tiff file or some other error occurred. Error: " + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    Else
        MessageBox.Show("The source file can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'Releasing resources.
    oGdPictureImaging.TiffCloseMultiPageFile(destImageId)
    oGdPictureImaging.ReleaseGdPictureImage(destImageId)
    oGdPictureImaging.ReleaseGdPictureImage(sourceImageId)
    oGdPictureImaging.Dispose()
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    GdPictureImaging oGdPictureImaging = new GdPictureImaging();
    //Variable to hold the source image handle (source file).
    int sourceImageId = 0;
    //Variable to hold the destination image handle (destination file).
    int destImageId = 0;
    //Variable to hold the imageID for the extracted page.
    int imageId = 0;
    //The source file path and the destination file path.
    string source = "source.tif", destination = "destination.tif";
    //Loading the source image file.
    sourceImageId = oGdPictureImaging.CreateGdPictureImageFromFile(source);
    if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)
    {
        bool isMultiPage = oGdPictureImaging.TiffIsMultiPage(sourceImageId);
        GdPictureStatus status = oGdPictureImaging.GetStat();
        if ((status == GdPictureStatus.OK) && isMultiPage)
        {
            int pagesCount = oGdPictureImaging.TiffGetPageCount(sourceImageId);
            //Setting the page numbers you want to extract.
            int[] pages = { 1, 3, 5};
            //Ensuring that the multi-page tiff source file includes the required pages.
            if (pagesCount>=5)
            {
                bool firstPage = true;
                foreach (int page in pages)
                {
                    //Temporary stream for the extracted page.
                    Stream pStream = new MemoryStream();
                    status = oGdPictureImaging.TiffExtractPage(sourceImageId, page, pStream);                           
                    if (status == GdPictureStatus.OK)
                    {
                        imageId = oGdPictureImaging.CreateGdPictureImageFromStream(pStream);
                        status = oGdPictureImaging.GetStat();
                        if (status == GdPictureStatus.OK)
                        {
                            if (firstPage)
                            {
                                //If it is the first image, then create a new multipage tiff file.
                                destImageId = imageId;
                                status = oGdPictureImaging.TiffSaveAsMultiPageFile(destImageId, destination, TiffCompression.TiffCompressionAUTO);
                                if (status != GdPictureStatus.OK) break;
                                firstPage = false;
                            }
                            else
                            {
                                //If it is the second image or more, add it to the previously created multipage tiff file.
                                status = oGdPictureImaging.TiffAddToMultiPageFile(destImageId, imageId);
                                if (status != GdPictureStatus.OK) break;
                                //Releasing the current image to minimize the memory usage.
                                oGdPictureImaging.ReleaseGdPictureImage(imageId);
                            }
                        }
                        else break;
                    }
                    else break;
                    pStream.Dispose();
                }
                if (status == GdPictureStatus.OK)
                    MessageBox.Show("Done!", "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show("Error: " + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                MessageBox.Show("Not enough pages to extract.", "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
            MessageBox.Show("Either the source file is not a multi-page tiff file or some other error occurred. Error: " + status.ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
        MessageBox.Show("The source file can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Multipage TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    //Releasing resources.
    oGdPictureImaging.TiffCloseMultiPageFile(destImageId);
    oGdPictureImaging.ReleaseGdPictureImage(destImageId);
    oGdPictureImaging.ReleaseGdPictureImage(sourceImageId);
    oGdPictureImaging.Dispose();