In This Topic
Programming / TIFF / Appending images from different sources to a single or a multipage TIFF file

Appending images from different sources to a single or a multipage TIFF file

In This Topic

Appending images from files of different formats to an existing single or multipage TIFF file is constantly one of the most required features. Here you can find the quick way how to accomplish this task very easy.

  • The first example shows you how to append modified pages from a multipage TIFF file to another existing TIFF file.
    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    Using oGdPictureImaging As GdPictureImaging = New GdPictureImaging()
        oGdPictureImaging.TiffOpenMultiPageForWrite(True)
        'Load your source image here, for example a multipage TIFF file.
        Dim ImageID As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("input_multipage.tif", True)
        If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then
            Dim PageCount As Integer = oGdPictureImaging.TiffGetPageCount(ImageID)
            Dim status As GdPictureStatus = GdPictureStatus.OK
            If PageCount > 0 Then
                Dim ImgTiffCompression As TiffCompression = oGdPictureImaging.GetTiffCompression(ImageID)
                For i As Integer = 1 To PageCount
                    status = oGdPictureImaging.TiffSelectPage(ImageID, i)
                    If status = GdPictureStatus.OK Then
                        'Modify the source image according to your needs.
                        status = oGdPictureImaging.DrawText(ImageID, "Page " + i.ToString() + " / " + PageCount.ToString(), 50, 50, 10, GdPicture14.FontStyle.FontStyleRegular, Color.Black, "Arial", True)
                        If status = GdPictureStatus.OK Then status = oGdPictureImaging.AppendToTiff(ImageID, "output_existing.tif", ImgTiffCompression)
                        If status <> GdPictureStatus.OK Then Exit For
                    Else
                        Exit For
                    End If
                Next
            Else
                'The source file is not a multipage file.
                status = oGdPictureImaging.DrawText(ImageID, "Page 1/1", 50, 50, 10, GdPicture14.FontStyle.FontStyleRegular, Color.Black, "Arial", True)
                If status = GdPictureStatus.OK Then status = oGdPictureImaging.AppendToTiff(ImageID, "output_existing.tif", TiffCompression.TiffCompressionAUTO)
            End If
            If status = GdPictureStatus.OK Then
                MessageBox.Show("The process has finished successfully.", "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MessageBox.Show("The process has failed. Status: " + status.ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            oGdPictureImaging.ReleaseGdPictureImage(ImageID)
        Else
            MessageBox.Show("The source file can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Using
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    using (GdPictureImaging oGdPictureImaging = new GdPictureImaging())
    {
        oGdPictureImaging.TiffOpenMultiPageForWrite(true);
        //Load your source image here, for example a multipage TIFF file.
        int ImageID = oGdPictureImaging.CreateGdPictureImageFromFile("input_multipage.tif", true);
        if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)
        {
            int PageCount = oGdPictureImaging.TiffGetPageCount(ImageID);
            GdPictureStatus status = GdPictureStatus.OK;
            if (PageCount > 0)
            {
                TiffCompression ImgTiffCompression = oGdPictureImaging.GetTiffCompression(ImageID);
                for (int i = 1; i <= PageCount; i++)
                {
                    status = oGdPictureImaging.TiffSelectPage(ImageID, i);
                    if (status == GdPictureStatus.OK)
                    {
                        //Modify the source image according to your needs.
                        status = oGdPictureImaging.DrawText(ImageID, "Page " + i.ToString() + " / " + PageCount.ToString(), 50, 50, 10, GdPicture14.FontStyle.FontStyleRegular, Color.Black, "Arial", true);
                        if (status == GdPictureStatus.OK)
                            status = oGdPictureImaging.AppendToTiff(ImageID, "output_existing.tif", ImgTiffCompression);
                        if (status != GdPictureStatus.OK) break;
                    }
                    else break;
                }
            }
            else
            {
                //The source file is not a multipage file.
                status = oGdPictureImaging.DrawText(ImageID, "Page 1/1", 50, 50, 10, GdPicture14.FontStyle.FontStyleRegular, Color.Black, "Arial", true);
                if (status == GdPictureStatus.OK)
                    status = oGdPictureImaging.AppendToTiff(ImageID, "output_existing.tif", TiffCompression.TiffCompressionAUTO);
            }
            if (status == GdPictureStatus.OK)
                MessageBox.Show("The process has finished successfully.", "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("The process has failed. Status: " + status.ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
            oGdPictureImaging.ReleaseGdPictureImage(ImageID);
        }
        else MessageBox.Show("The source file can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
  •  The second example demonstrates the very simple way how to append JPEG images to a new or an existing TIFF file.
    Copy Code
    'We assume that GdPicture has been correctly installed and unlocked.
    Using dialog As FolderBrowserDialog = New FolderBrowserDialog()
        dialog.Description = "Select the folder you want to append your files from."
        dialog.ShowNewFolderButton = False
        dialog.RootFolder = Environment.SpecialFolder.Desktop
        If dialog.ShowDialog() = DialogResult.OK Then
            Dim status As GdPictureStatus = GdPictureStatus.OK
            Dim folder As String = dialog.SelectedPath
            For Each fName As String In Directory.GetFiles(folder, "*.jpg", SearchOption.TopDirectoryOnly)
                Using oGdPictureImaging As GdPictureImaging = New GdPictureImaging()
                    Dim ImageID As Integer = oGdPictureImaging.CreateGdPictureImageFromFile(fName, True)
                    If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then
                        'If the resulting file does not exist, the toolkit will create a new one.
                        status = oGdPictureImaging.AppendToTiff(ImageID, "output.tif", TiffCompression.TiffCompressionJPEG, 95)
                        oGdPictureImaging.ReleaseGdPictureImage(ImageID)
                        If status <> GdPictureStatus.OK Then
                            MessageBox.Show("The source file " + fName + " can't be appended. Status: " + oGdPictureImaging.GetStat().ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Exit For
                        End If
                    Else
                        MessageBox.Show("The source file " + fName + " can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
                End Using
            Next
            If status = GdPictureStatus.OK Then MessageBox.Show("The process has finished successfully.", "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Using
    Copy Code
    //We assume that GdPicture has been correctly installed and unlocked.
    using (FolderBrowserDialog dialog = new FolderBrowserDialog())
    {
        dialog.Description = "Select the folder you want to append your files from.";
        dialog.ShowNewFolderButton = false;
        dialog.RootFolder = Environment.SpecialFolder.Desktop;
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            GdPictureStatus status = GdPictureStatus.OK;
            string folder = dialog.SelectedPath;
            foreach (string fName in Directory.GetFiles(folder, "*.jpg", SearchOption.TopDirectoryOnly))
            {
                using (GdPictureImaging oGdPictureImaging = new GdPictureImaging())
                {
                    int ImageID = oGdPictureImaging.CreateGdPictureImageFromFile(fName, true);
                    if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)
                    {
                        //If the resulting file does not exist, the toolkit will create a new one.
                        status = oGdPictureImaging.AppendToTiff(ImageID, "output.tif", TiffCompression.TiffCompressionJPEG, 95);
                        oGdPictureImaging.ReleaseGdPictureImage(ImageID);
                        if (status != GdPictureStatus.OK)
                        {
                            MessageBox.Show("The source file " + fName + " can't be appended. Status: " + oGdPictureImaging.GetStat().ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            break;
                        }
                    }
                    else MessageBox.Show("The source file " + fName + " can't be loaded. Status: " + oGdPictureImaging.GetStat().ToString(), "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (status == GdPictureStatus.OK)
                MessageBox.Show("The process has finished successfully.", "Append To TIFF Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }