Exception combining TIFF images

Attached is the Visual Studio Solution.
ImageMan64Test.zip (15.0 KB)

fpDest - file path to the file that will hold the combined files.
safp - array of files to combine

In the example program, a directory is entered in the UI. When the Combine button is clicked, all the .tif files in the directory gets combined into a file called AsposeCombined.tif.

In this part of the code:
using (FileStream outputStream = new FileStream(fpDest, FileMode.Create, FileAccess.Write))
{
image1.Save(outputStream, outputSettings);
outputStream.Close();
}

the image1.Save call throws the following exception:

Cannot access a disposed object.
Object name: ‘TiffFrame’. Shipment ID: 22399504. Stack Trace: at Aspose.Imaging.DisposableObject.VerifyNotDisposed()
at .(TiffOptions , Int32[][] , Rectangle , Boolean )
at … (Rectangle )
at ​ .( , IList1 , ) at ​ .(Rectangle , , , Int32 , Int32 , ) at . (TiffStreamReader , Rectangle , IPartialArgb32PixelLoader ) at ​ .LoadPartialArgb32Pixels(Rectangle , IPartialArgb32PixelLoader ) at ​ . (Rectangle ) at ​ .( , IList1 , )
at ​ .(Rectangle , , , Int32 , Int32 , )
at . (Rectangle , IPartialArgb32PixelLoader )
at Aspose.Imaging.RasterImage.(Rectangle , IPartialArgb32PixelLoader )
at Aspose.Imaging.RasterImage.LoadPartialArgb32Pixels(Rectangle rectangle, IPartialArgb32PixelLoader partialPixelLoader)
at Aspose.Imaging.FileFormats.Tiff.TiffFrame.(RasterImage , TiffStreamWriter , Boolean , TiffOptions , ExifData , XmpPacketWrapper , Rectangle )
at Aspose.Imaging.FileFormats.Tiff.TiffImage.(TiffStreamWriter )
at Aspose.Imaging.FileFormats.Tiff.TiffImage.SaveData(Stream stream)
at Aspose.Imaging.DataStreamSupporter.Save(Stream stream)
at Aspose.Imaging.DataStreamSupporter.Save(String filePath, Boolean overWrite)
at ImageManager3.ImageUtil.CombineImageFiles(String fpDest, String[] safp, Int32[])

This seems to happen when a lot of files are combined. I don’t know if it’s a totally filesize issue or if it’s the number of files. Usually combining just a few files works.

@yangh1,

I have worked with the sample project shared by you using Aspose.Imaging for .NET 20.3. Unfortunately, I am unable to find source files reproducing the issue on your end. Please share the requested files and also give a try to Aspose.Imaging for .NET 20.3 before sharing with us.

CombineTest.zip (6.6 MB)

I have attached the test files that I used to reproduce the issue. If you still do not see the same behavior, just copy the attached files multiple times in the same directory. I did also try using .NET 20.3 and got the same exception above.

Thank you.

@yangh1,

I have worked with sample files shared and have been able to observe the issue. An issue with ID IMAGINGNET-3820 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

The issue status says that it has been resolved. Is there a new release for the fix?

@yangh1,

The issue has been marked as resolved in upcoming Aspose.Imaging for .NET 20.5. We will share the notification with you as soon as the product wil be shared online by end of this month.

The issues you have found earlier (filed as IMAGINGNET-3820) have been fixed in this update.

I am unfortunately still having problems with combining a large number of TIFF files. I have even tried using the latest release 20.8. Using the same code, I am getting the following exception:

Image export failed. Cannot access a disposed object.
Object name: ‘TiffFrame’.

at Aspose.Imaging.Image.Save(Stream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)
at Aspose.Imaging.RasterImage.Save(Stream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)
at Aspose.Imaging.Image.Save(Stream stream, ImageOptionsBase optionsBase)
at ImageMan64Test.AsposeFunc.CombineImageFiles(String fpDest, String[] safp, Int32[]& nPageCounts) in C:\dev\Testing\ImageManvsAsposeTest\ImageMan64Test\AsposeFunc.cs:line 408
at ImageMan64Test.frmTestImageMan.btnAspose_Click(Object sender, EventArgs e) in C:\dev\Testing\ImageManvsAsposeTest\ImageMan64Test\Form1.cs:line 75
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at ImageMan64Test.Program.Main() in C:\dev\Testing\ImageManvsAsposeTest\ImageMan64Test\Program.cs:line 19

Another thing that I tried was removing the TiffOptions parameter in the Save function. I still got the same exception, but the stack trace was a little different:

at Aspose.Imaging.DisposableObject.VerifyNotDisposed()
at .(​ , Int32[][] , Rectangle , Boolean )
at . ​ ​ (Rectangle )
at .( ​ , IList1 , ) at .(Rectangle , ​ , , Int32 , Int32 , ) at . ​ (TiffStreamReader , Rectangle , IPartialArgb32PixelLoader ) at ​ .LoadPartialArgb32Pixels(Rectangle , IPartialArgb32PixelLoader ) at . ​ ​ (Rectangle ) at .( ​ , IList1 , )
at .(Rectangle , ​ , , Int32 , Int32 , )
at . ​ (Rectangle , IPartialArgb32PixelLoader )
at Aspose.Imaging.RasterImage.(Rectangle , IPartialArgb32PixelLoader )
at Aspose.Imaging.RasterImage.LoadPartialArgb32Pixels(Rectangle rectangle, IPartialArgb32PixelLoader partialPixelLoader)
at Aspose.Imaging.FileFormats.Tiff.TiffFrame.(RasterImage , TiffStreamWriter , Boolean , TiffOptions , ExifData , XmpPacketWrapper , Rectangle )
at Aspose.Imaging.FileFormats.Tiff.TiffImage.(TiffStreamWriter )
at Aspose.Imaging.FileFormats.Tiff.TiffImage.SaveData(Stream stream)
at Aspose.Imaging.DataStreamSupporter.Save(Stream stream)
at ImageMan64Test.AsposeFunc.CombineImageFiles(String fpDest, String[] safp, Int32[]& nPageCounts) in C:\dev\Testing\ImageManvsAsposeTest\ImageMan64Test\AsposeFunc.cs:line 408
at ImageMan64Test.frmTestImageMan.btnAspose_Click(Object sender, EventArgs e) in C:\dev\Testing\ImageManvsAsposeTest\ImageMan64Test\Form1.cs:line 75
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at ImageMan64Test.Program.Main() in C:\dev\Testing\ImageManvsAsposeTest\ImageMan64Test\Program.cs:line 19

@yangh1

We are sorry for your inconvenience. In that case please provide the set of files along with used code so that we may evaluate it further on our end to help you.

CombineTest4.zip (7.8 MB)
CombineTestCode.zip (15.3 KB)
I have attached the code as well as the directory of files that were used in the test.

@yangh1

I have created an issue with ID IMAGINGNET-4155 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

The issues you have found earlier (filed as IMAGINGNET-4155) have been fixed in this update.

@yangh1

By using Aspose.Imaging for .NET 20.12 the issue is not reproduced. Can you please try using following sample code on your end.

using (var page1 = (TiffImage)Image.Load("Image1.tif"))
{
    using (var page2 = (TiffImage)Image.Load("Image2.tif"))
    {
        page1.AddFrame(TiffFrame.CopyFrame(page2.ActiveFrame));
    }

    page1.Save("Result.tif");
}

Samples files.zip (2.7 MB)

How do you handle multi-paged TIFF files?

@yangh1

The problem with the original code is that the source Tiff images were disposed before they were used in the Save method. In the example below, this issue does not appear (by setting null value to the XmpData property). Can you please try following correct code for your utility, this is a method from your AsposeFunc.cs file.

  public static void CombineImageFiles(string fpDest, string[] safp)
        {
            safp = safp.Where(File.Exists).ToArray();
            if (safp.Length == 0)
            {
                return;
            }

            using (TiffImage image = (TiffImage)Image.Load(safp[0]))
            {
                List<IDisposable> srcImages = new List<IDisposable>();
                for (int fileIndex = 1; fileIndex < safp.Length; fileIndex++)
                {
                    TiffImage srcImage = (TiffImage)Image.Load(safp[fileIndex]);
                    srcImages.Add(srcImage);
                    foreach (TiffFrame srcFrame in srcImage.Frames)
                    {
                        // removing XMP data to prevent System.MissingMethodException
                        srcFrame.XmpData = null;
                        TiffFrame frame = TiffFrame.CopyFrame(srcFrame);
                        image.AddFrame(frame);
                    }
                }

                try
                {
                    TiffOptions outputSettings = new TiffOptions(TiffExpectedFormat.Default);
                    ResolutionSetting res = new ResolutionSetting(300, 300);  // 300 x 300 DPI    
                    outputSettings.BitsPerSample = new ushort[] { 4 };
                    outputSettings.Compression = TiffCompressions.Lzw;
                    outputSettings.Photometric = TiffPhotometrics.Palette;
                    outputSettings.Palette = ColorPaletteHelper.Create4BitGrayscale(false);
                    outputSettings.ResolutionSettings = res;

                    // Setting empty page exporting action to activate the batch export mode (needed for economical use of memory)
                    // It is available in Aspose.Imaging 20.3+
                    image.PageExportingAction = (index, page) => { };

                    image.Save(fpDest, outputSettings);
                }
                finally
                {
                    foreach (IDisposable srcImage in srcImages)
                    {
                        srcImage.Dispose();
                    }
                }
            }
        }