Page 1 of 1

Help with merging TIFF files #2

Posted: 2019-05-02T05:51:20-07:00
by SofiaRodrigues25
I need help merging TIFF files (again).

I want to use Magick.NET because I want the compression type to be JPEG and the System.Drawing.Imaging doesn't have that type.

The problem when it executes the line

Code: Select all

 images.Write(outFile); 
I get the error:
System.ObjectDisposedException: Cannot access a disposed object.
But the line is inside a using block and I never dispose that.

My code:

Code: Select all

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++)
                    {
                        bool isError = false;
                        try
                        {
                            using (var image = new MagickImage(File.ReadAllBytes(imageFiles[i]), settings))
                            {
                                image.Settings.Compression = CompressionMethod.JPEG;
                                // Add the image
                                images.Add(image);
                            }

                        }
                        catch (Exception ex)
                        {
                            if (ex is System.IO.IOException)
                                MessageBox.Show("O ficheiro '" + imageFiles[i] + "' já existe na pasta escolhida.");
                            else
                                MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK); 
                            isError = true;
                        }
                        if (isError) continue;
                    }

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

Re: Help with merging TIFF files #2

Posted: 2019-05-02T14:10:17-07:00
by dlemstra
You should not be using the `using` statement for images that you add to the collection. When you add them the collection (images) takes ownership and will Dispose them when you Dispose the collection.

Re: Help with merging TIFF files #2

Posted: 2019-05-03T09:47:27-07:00
by SofiaRodrigues25
Indeed, it was it.

But theres another thing... The files are not merging even if I use the Combine() method.
So, how can I merge them like, if I have 10 TIFFs, make it 1 TIFF with 10 pages?

Re: Help with merging TIFF files #2

Posted: 2019-05-03T12:08:05-07:00
by dlemstra
Your code should do that now you are adding the pages to the collection and when you write it it should create a tiff image with 10 pages (when you add 10 images to the collection.

Re: Help with merging TIFF files #2

Posted: 2019-05-09T03:19:26-07:00
by SofiaRodrigues25
Thank you, it did work.