Combine TIFF files in C#

Asked

Viewed 174 times

1

I’m trying to combine multiple TIFF files into a multipage. I am used Magick.NET but the end result is a TIFF with only the first page.

I tested with a 10-page file but only got one. I think there is a problem with the line "images.Combine()" but I’m not sure. So, what I have changes in the code so that I combine the files correctly?

Code:

    public void JoinTiffJPEG(string[] imageFiles, string outFile)
    {
        using (MagickImageCollection images = new MagickImageCollection())
        {

            try
            {
                MagickReadSettings settings = new MagickReadSettings();
                settings.Compression = CompressionMethod.JPEG;

                for (int i = 0; i <= (imageFiles.Length - 1); i++)
                {
                    MagickImage image = new MagickImage(File.ReadAllBytes(imageFiles[i]), settings);
                    image.Settings.Compression = CompressionMethod.JPEG;
                    // Add the image
                    images.Add(image);

                }
                images.Combine();
                Stream output = new FileStream(outFile, FileMode.Create);
                images.Write(output, MagickFormat.Tif);

                //images.Dispose();
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
            }
        }
    }

This is the content of Magickimagecollection after saving the 10 pages of the file: Coleção de imagens

  • @rnd_rss I published both and they are different issues. Read both ;) This is about the act of joining the Tiffs and the other era because it gave error in one line of code.

  • You debugged the code?

  • Yes @Leandroangelo and the MagickImageCollection images actually keeps the images there, but when I save and do "Write" it doesn’t keep all the pages.

  • @Leandroangelo added a debug image.

2 answers

1


After putting the question also on the Magick.NET support site and with the help of @Lucasmiranda, I came to the conclusion that the solution is simpler than that.

So the code stays:

public void JoinTiffJPEG(string[] imageFiles, string outFile)
{
    using (MagickImageCollection images = new MagickImageCollection())
    {

        try
        {
            MagickReadSettings settings = new MagickReadSettings();
            settings.Compression = CompressionMethod.JPEG;
            for (int i = 0; i <= (imageFiles.Length - 1); i++)
            {
                MagickImage image = new MagickImage(File.ReadAllBytes(imageFiles[i]), settings);
                image.Settings.Compression = CompressionMethod.JPEG;
                // Add the image
                images.Add(image);
            }
            images.Write(outFile); 
            return;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
        }
    }
} 

1

What you’re forgetting is that the merge() method and the combine() return an image, and you’re not assigning anywhere, would be that image you should write to get the combined result, below:

public void JoinTiffJPEG(string[] imageFiles, string outFile)
    {
        using (MagickImageCollection images = new MagickImageCollection())
        {

            try
            {
                MagickReadSettings settings = new MagickReadSettings();
                settings.Compression = CompressionMethod.JPEG;

                for (int i = 0; i <= (imageFiles.Length - 1); i++)
                {
                    MagickImage image = new MagickImage(File.ReadAllBytes(imageFiles[i]), settings);
                    image.Settings.Compression = CompressionMethod.JPEG;
                    // Add the image
                    images.Add(image);

                }
                var imagemCombinada = images.Combine();  //atribuição do resultado
                Stream output = new FileStream(outFile, FileMode.Create);
                imagemCombinada.Write(output, MagickFormat.Tif); //escrevendo resultado combinado

                //images.Dispose();
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
            }
        }
    }

--Update, if you want to combine them one below the other or side by side the command you are looking for is Appendhorizontally and Appendvertically, example:

public void JoinTiffJPEG(string[] imageFiles, string outFile)
        {
            using (MagickImageCollection images = new MagickImageCollection())
            {

                try
                {
                    MagickReadSettings settings = new MagickReadSettings();
                    settings.Compression = CompressionMethod.JPEG;

                    for (int i = 0; i <= (imageFiles.Length - 1); i++)
                    {
                        MagickImage image = new MagickImage(File.ReadAllBytes(imageFiles[i]), settings);
                        image.Settings.Compression = CompressionMethod.JPEG;
                        // Add the image
                        images.Add(image);

                    }
                    var imagemCombinadaNaVertical = images.AppendVertically();  //atribuição do resultado
                    Stream output = new FileStream(outFile, FileMode.Create);
                    imagemCombinadaNaVertical .Write(output, MagickFormat.Tif); //escrevendo resultado combinado

                    //images.Dispose();
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
                }
            }
        }
  • Thank you for the reply @Lucasmiranda , but the result gave an image superimposed with others... see here: https://ibb.co/SvYRCkJ

  • 1

    I get it, you want them to be below each other is this?

  • Right @Lucasmiranda , I want to get a multipage TIFF, for example, I’m converting a PDF to TIFF and this PDF has 10 pages, so TIFF will get 10 pages too, but with JPEG compression.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.