Free Support Forum - aspose.com

Files not being released from stream (C# .NET)

Below is the code that I am running to combine multiple TIFF files. The problem that I am running into is that files remain locked, and the following exception is thrown:

The process cannot access the file ‘C:\temp\test\testTIFF\CombineTest2\AsposeCombined.tif’ because it is being used by another process.

I have all the filestreams within using statements, and all the filestreams are explicitly closed. The problem seems to be with the final save function because if I comment that part out, the iteration of the files and frames work fine. Is there something that I’m missing?

    public static void CombineImageFiles(string fpDest, string[] safp)
    {
        if (safp == null || safp.GetLength(0) < 1)
            return;

        int nFirstFile = 0;
        bool bDone = false;
        do
        {
            if (!File.Exists(safp[nFirstFile]))
                nFirstFile++;
            else
                bDone = true;

        } while (!bDone && nFirstFile < safp.Length);

        if (!bDone)
            return;

        using (FileStream fileStream1 = new FileStream(safp[0], FileMode.Open))
        {
            using (TiffImage image1 = (TiffImage)Image.Load(fileStream1))
            {
                for (int i = nFirstFile + 1; i < safp.Length; ++i)
                {
                    string fp = safp[i];

                    if (!File.Exists(fp))
                        continue;

                    using (FileStream fileStream = new FileStream(fp, FileMode.Open))
                    {
                        using (TiffImage image = (TiffImage)Image.Load(fileStream))
                        {
                            int nPageCount = image.Frames.Length;
                            for (int p = 0; p < nPageCount; p++)
                            {
                                // Create an instance of TIffFrame and copy frame of source image
                                TiffFrame frame = TiffFrame.CopyFrame(image.Frames[p]);

                                // Add copied frame to destination image
                                image1.AddFrame(frame);
                            }
                        }
                        fileStream.Close();                            
                    }
                }

                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;

                using (FileStream outputStream = new FileStream(fpDest, FileMode.Create, FileAccess.Write))

                {
                    image1.Save(outputStream, outputSettings);
                    outputStream.Close();
                }
            }

            // Save the image with changes            
            fileStream1.Close();                
        }
    }

@yangh1,

I have observed the issue shared by you and have created an issue with ID IMAGINGNET-3750 as investigation in our issue tracking system. In the mean while, I suggest you to please try copying the FileStream to MemoryStrem in your application and use that. I hope this shall avoid the issue you are facing.

@yangh1,

We have tried to investigate the issue on our end and have not been able to proceed further on our end. Actually there is a method in the task - public static void CombineImageFiles(string fpDest, string[] safp)

CombineImageFiles (???, ???)

Can you please explain what do you pass into this method as parameters? If possible, please provide a working Visual Studio Solution project that we may try on our end to verify. That will be resolving all dependencies as well as missing calls.

ImageMan64Test.zip (15.0 KB)

Attached is the Visual Studio Solution.

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.

Another issue that we have started seeing when using this function is this 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[])

Unfortunately, it is not always reproducible.

@yangh1,

Thank you for sharing the information with us. We will share feedback with you as soon as issue will be addressed.