Error unable to access C#image collection

Asked

Viewed 47 times

2

I am using Magick.NET to choose JPEG compression for my TIFF files. So, I save each one with this compression in a collection of images. Finally I want to join all the TIFF of the collection in one file.

But when I do "images. Match();" I get the error:

Erro

I also tried to do the same action without the above mentioned line of code but I get the same error. However, I don’t see where the object was Disposed because everything is inside a block using.

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++)
            {
                using (var 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);
        }
    }
}
  • 1

    Throw away the using, on the line using (var image = ...).

  • @Marceloshinitiuchimura That’s it, thank you! However, the file only has the first page and not all... Can you help me with this or should I post a new question?

  • New question, if you please.

  • Okay. Thank you.

  • @Marceloshinitiuchimura Please put this in the answer. And Sofia, when there is the answer, please mark as solved. : ) This can help others to have the same doubt.

  • All right, I’ll do it.

Show 1 more comment

1 answer

2


With the help of @Marceloshinitiuchimura’s comment, the answer is to remove the block using on the line

using (var image = new MagickImage(File.ReadAllBytes(imageFiles[i]), settings))

So the final code will remain:

    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);
            }
        }
    }

Browser other questions tagged

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