Concatenating and Processing Tiffs

Our document management system requires that all our documents are generated as tiff images. We are using Aspose to convert several types of documents to tiff. Currently, we are working on a way to concatenate tiff images into one tiff file. We are having an issue concatenating non-Aspose generated tiffs with Aspose generated tiffs. How can we process the non-Aspose generated tiff images so that we can concatenate them?


Have tried applying the following to no avail:
using (TiffImage image = (TiffImage)Aspose.Imaging.Image.Load(file))
{
//Export to TIFF file format using the default options
image.Save( RenameToTif(file), new Aspose.Imaging.ImageOptions.TiffOptions());
}

Hi Jane,

Thank you for using Aspose products.

If you wish to concatenate only Tiff images, as you have mentioned in your post then you can simply copy several Tiff Frames to one Tiff file. Please check the below provided simplest code to achieve this.

C#


//Create an instance of TiffImage and load the destination image
using (TiffImage image = (TiffImage)Aspose.Imaging.Image.Load(myDir + “destination.tif”))
{
//Create an instance of TiffImage and load the source image
using (TiffImage image1 = (TiffImage)Aspose.Imaging.Image.Load(myDir + “source.tif”))
{
// Create an instance of TIffFrame and copy active frame of source image
TiffFrame frame = TiffFrame.CopyFrame(image1.ActiveFrame);
// Add copied frame to destination image
image.AddFrame(frame);
// save the image with changes
image.Save();

}
}

In case you face any difficulties, please share your sample images for our review.

That isn’t our issue. I’ve already got that part working successfully and tested. The problem that I am having is that I am unable to concatenate an image generated from aspose with one generated with our document management system. Because of privacy laws I cannot share the files with you. I am assuming that there is some issue with incompatible settings on the files. I would like to create some sort of “clearing house” that would allow me to ensure that all the images are of the same settings. In essence converting each tiff to make sure that they are all aspose tiffs. How would I accomplish this?

Also have tried

 using (TiffImage tiffImage = (TiffImage)Aspose.Imaging.Image.Create(outputSettings, newWidth, newHeight))
{
int index = 0;
foreach (var file in files)
{
using (RasterImage ri = (RasterImage)Aspose.Imaging.Image.Load(file))
{
ri.Resize(newWidth, newHeight, ResizeType.NearestNeighbourResample);
TiffFrame frame = tiffImage.ActiveFrame;
if (index > 0)
{
frame = new TiffFrame(new TiffOptions(outputSettings) /ensure options are cloned for each frame/,
newWidth, newHeight);
// If there is a TIFF image loaded you need to enumerate the frames and perform the following
// frame = TiffFrame.CreateFrameFrom(sourceFrame, outputSettings);
}
                    frame.SavePixels(frame.Bounds, ri.LoadPixels(ri.Bounds));

                    <span style="color:blue;">if</span> (index > 0)
                    {
                        tiffImage.AddFrame(frame);
                    }

                    index++;
                }
            }

            tiffImage.Save(path);
        }</pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial;"><br></pre><pre style="font-family: Consolas; font-size: 13px; background-color: white; background-position: initial initial; background-repeat: initial initial;">Null error was thrown on:  ri.LoadPixels(ri.Bounds)</pre></div>

Hi Jane,

Thank you for further elaboration.

Please check the alternative for your recently shared source code.

C#


//Create an instance of TiffImage and load an existing image
using (TiffImage multiImage = (TiffImage)Aspose.Imaging.Image.Load(myDir + “mytiff2.tif”))
{
//Create an instance of int to keep track of frames in TiffImage
int frameCounter = 0;

//Iterate over the TiffFrames in TiffImage
foreach (TiffFrame tiffFrame in multiImage.Frames)
{
//Set the current frame to be active
multiImage.ActiveFrame = tiffFrame;

//Create an instance of TiffOptions
var createOptions = new Aspose.Imaging.ImageOptions.TiffOptions();

//Set the Source of createOptions as FileCreateSource by specifing the location where output will be saved
createOptions.Source = new FileCreateSource(string.Format(“{0}\Concat{1}.tif”, myDir, frameCounter), false);

//Create a new TiffImage having the dimensions of current active frame
using (var tiffImage = (TiffImage)Image.Create(createOptions, tiffFrame.Width, tiffFrame.Height))
{
//Create a copy of active frame to be added to another image
TiffFrame frame = TiffFrame.CopyFrame(tiffFrame);
//Add frame to output image
tiffImage.AddFrame(frame);
//Set the current frame to be active frame for output image
tiffImage.ActiveFrame = frame;
//Remove the first empty frame; By default an empty frame is added to every Tiff image
tiffImage.RemoveFrame(0);
//Save the output image
tiffImage.Save();
}
frameCounter++;
}
}


Above code loads a Tiff image, and saves its frames to individual Tiff files. This way you will process each file with Aspose.Imaging component, assuring that all images are compatible for concatenation. You can then further process these separate frames to be concatenated in a single file as per your requirement.

In your first post, you have mentioned that you are having issue while concatenating non-Aspose generated images with Aspose generated images. Could you please elaborate these problems? As far as I have tried, I am able to concatenate Aspose generated images with the ones downloaded from internet, without observing any exceptions or bad results. Please note that I am using the assemblies from the latest version of Aspose.Imaging for .NET v2.0.0.

If you are facing any compatibility issues then it is essential for us to study these problematic images. in order to make sure that we are delivering a universal solution. Please be assured that anything you share with us (via private post or email) will not be disclosed to anyone or any party but Aspose team. We collect these samples to re-produce any issue on our end, and we delete such data as soon as the issue gets resolved.

We are having an issue with that code. I set the code you provided inside a loop to process all the files in a file folder. When the process you sent finishes processing a (n)-paged file, let's call it "OriginalTiff.tif", it creates "OriginalFile(n).tiff" which is an individual file for each page of the original multi-paged tiff. I.e. "OriginalFile1.tif" is page 1 of "OriginalFile.tif". I would like one tiff file to be created as opposed to several.

The other issues is:

When it finishes processing "OriginalFile.tif" the code then stops processing then it puts a hold on the first image "OriginalFile1.tif" and then never releases it. Which doesn't allow the code to move to process the next file in the folder i.e. After creating "OriginalFile1.tif" and releasing it to create "OriginalFile2.tif" (or "OriginalFile(n).tif") it then picks "OriginalFile1.tif" back into memory then freezes and then "SecondOriginalFile.Tif" never gets processed.

What can I do to fix this so that "SecondOriginalFile.Tif" can get processed. Code examples to come later.

Ok, after a lot of time the files did concatenate however it took a very long time and the files did not completely generate. A large portion of each page of the combined document are partially blacked out. What can we do to speed up the process and what can we do to have images that aren’t blackened out?

Hi Jane,

I am afraid; without reviewing your sample files and the source code, we’re unable to analyze the problem cause for the issue as discussed in your recent post. I am not experiencing any bad results on my end therefore I would request you again to please provide a couple of your sample files for our review. These files don’t need to have any private information on them. You can easily generate images with dummy data by using your document management system, and share them here. If you like, you can email us your samples or I can make the post private so that no one but Aspose team members and the owner (you) could have access to the thread attachments. If you need further assurance regarding the privacy of your data, please go through the EULA for NDA (clause 8).

Please note that the time of execution mainly depends upon the number of files, their sizes, resolution and number of frames per file. Moreover, you are involving an extra step in your program, that is to first process your Non-Aspose generated Tiff images with Aspose.Imaging component, just to make sure that they are compatible for concatenation. This overhead involves several extra IO operations, therefore the time of execution may increase exponentially by adding more file to your pool. You may give a try by not writing the intermediate files to disk, alternatively save them in the memory, and load again for concatenation.

In my understanding, your main goal is to concatenate several Tiff images to one Tiff file. Source code to achieve this requirement has already been shared here. If you are having any issues with any of your files, please share the problem description along with a couple of sample images. Upon reviewing the samples, if we find any compatibility problems in our component we will take appropriate actions to fix it at earliest possible.

Thank you for your understanding and co-operation.