Combine PNG files

Hello,


I have a need to combine PNG files into a single multiTIFF file.

I have an automated process which has been working for a while, where I combine single page TIF files into a multiTIFF file using Aspose Imaginng.

I will be getting single page PNG files in some cases - and these need to be combined into one file as well.

Any suggestions?

Thanks

Prakash


Hi Prakash,

Thank you for your inquiry.

Please use the following code snippet to add/combine different images as separate frames in a multi page TIFF image. This code snippet iterate through a folder that contain images. The image count could be two or more and folder can contain PNG, JPG, BMP or JPEG images. You can alter this accordingly to your needs. For details please access the link:


CODE:

//Create an instance of TiffOptions with CCITTFAX3 compression
TiffOptions outputSettings = new TiffOptions(TiffExpectedFormat.TiffCcittFax4);
//Set source for the result
outputSettings.Source = new Aspose.Imaging.Sources.StreamSource(new MemoryStream());
//Declare Height and Width for the new TiffImage
int newWidth = 500;
int newHeight = 500;
string path = "output.tiff";
//Create an instance of TiffImage using the object of TiffOptions and dimension
using (TiffImage tiffImage = (TiffImage)Aspose.Imaging.Image.Create(outputSettings, newWidth, newHeight))
{
//Initialize a variable to keep track of frames in the TiffImage
int index = 0;
//Read all JPG files from any specified directory and iterate over the list
foreach (var file in Directory.GetFiles("someFolder\\", "*.jpg"))
{
//Load the image into an instance of RasterImage
using (RasterImage ri = (RasterImage)Aspose.Imaging.Image.Load(file))
{
//Resize the image according to TiffImage dimensions
ri.Resize(newWidth, newHeight, ResizeType.NearestNeighbourResample);
//Get the active frame of TiffImage
TiffFrame frame = tiffImage.ActiveFrame;
//Check if TiffImage already has a frame
if (index > 0)
{
//Create a new TiffFrame according to the TiffOptions settings
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); }
//Save the RasterImage data onto TiffFrame
frame.SavePixels(frame.Bounds, ri.LoadPixels(ri.Bounds));
if (index > 0)
{
//Add the newly created frame to the TiffImage
tiffImage.AddFrame(frame);
}
index++;
}
}
//Save the changes to TiffImage
tiffImage.Save(path);
}

Hope the above information helps. Feel free to contact us in case you have further query or comments.

Thanks for the help. I decided to go away from PNG to straight TIF and used the code you provided above. It works great most of the time.


During one test, I had 120 single page TIF files that I was creating a single MultiTif with when I got an error like this…
Cannot allocate so many bytes - use LoadPartialPixels instead…
Can you help with this?

Also, do you have a mechanism of determining when an input TIF file is CCITT (black and white) versus color - so I can use the CCiTT compression in place of the LZ (color) compression? I am assuming that they are all color so I can handle the few color files that come in. I was thinking that processing as color is a lot slower than processing B&W

Thanks

Prakash

Hi Prakash,

Thank you for writing us back.

Please forward us the sample problematic file. We will try to reproduce the mentioned issue at our end and update you with our findings via this forum thread.

It is very difficult to depict that an image is a black & white image or a color image. The reason is that if an image contains some pixels of RED color and rest of the image is black & while then you cannot say that it is a black & white image.

Furthermore Aspose.Imaging exposes a property called BitsPerPixel. It returns an integer value 1, 16, 24 or 32. Here value 1 means that it is a black & while image. Please use the following lines of code to get BitsPerPixel and to know the type of image. For details, please visit the link Manipulating TIFF Images.

sourceFilePath = @"F:\Ctrash\Input\allthepaperr.tif";
using (Aspose.Imaging.Image imageLoaded = Aspose.Imaging.Image.Load(sourceFilePath))
{
System.Console.WriteLine(imageLoaded.GetType().Name);
System.Console.WriteLine(imageLoaded.BitsPerPixel);
}

Hope the above information helps. Feel free to reach us in case of any query or comments.