Help with merging TIFF files #2

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
SofiaRodrigues25
Posts: 7
Joined: 2019-03-20T09:27:22-07:00
Authentication code: 1152

Help with merging TIFF files #2

Post 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);
                }
            }
        }
Sofia Rodrigues
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Help with merging TIFF files #2

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
SofiaRodrigues25
Posts: 7
Joined: 2019-03-20T09:27:22-07:00
Authentication code: 1152

Re: Help with merging TIFF files #2

Post 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?
Sofia Rodrigues
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Help with merging TIFF files #2

Post 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.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
SofiaRodrigues25
Posts: 7
Joined: 2019-03-20T09:27:22-07:00
Authentication code: 1152

Re: Help with merging TIFF files #2

Post by SofiaRodrigues25 »

Thank you, it did work.
Sofia Rodrigues
Post Reply