Manipulating TIFF file using JPEG compression

Hello Greetings!

I gone through the Aspose documents and I found Aspose.Imaging is supporting the JPEG compression somehow.

I am using Microsoft TiffCompressOption which provides only Ccitt3 ,Ccitt4 ,Lzw ,Rle ,Zip
TiffCompressOption Enum.

In my case, if I am trying to manipulate(such as rotate a page) the TIF file and saving it back then it is using LZW compression and the size of the file is increased massively, I want to use JPEG compression for this case.

can I integrate the Aspose.Imaging for handling the Tiff file only, I do not want to change existing code for other compression format, I want to integrate Aspose.Imaging only for Tiff perspective.

kindly let me know how it is possible to integrate the Aspose.Imaging in existing codebase.

Any suggestion for this ?

Thanks!
Ankit.

@ankitraman,

Can you please share source file along with complete working sample project so that we may further investigate to help you out.

@Adnan.Ahmad,

I need suggestions about how individual tiff pages is being manipulated and re-saved using Aspose.

I have tiff file with 4 pages and I want to rotate only some pages or all page by 90 degree clockwise and trying to re-save it in same tiff format using JPEG compression.

I need to change my codebase for only tiff files means if my code found that

Below are the Pseudocode for my expectation:

if(ItemType == Tiff File)
{
//perform tiff file manipulation using JPEG compression by Aspose library.
}
else
{
//perform manipulation using existing code.
//Here I want to use manipulation using my existing code for PNG,PDF,e.t.c.
}

Kindly let me know if more details needed.

@ankitraman,

I have observed your requirements. My request to you is to please provide the source file and desired output file that you want to generate using Aspose.Imaging. We will be able to proceed further with investigation on our end on provision of requested information.

Below are my demo code using Aspose:

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
namespace RotatePage
 {
  class Program
 {
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  static void Main(string[] args)
  {
     // Change the Path of Image file as per requirement.
     var stream = new FileStream(@"C:\New folder\img2.tif", FileMode.OpenOrCreate, FileAccess.ReadWrite);

     string directory = @"C:\Users\ankit.raman\Desktop\SavedImages";

     
     using (TiffImage tiffImage = (TiffImage)Image.Load(directory + "\\MultiPages.tif"))
     {
       
        foreach (TiffFrame tiffFrame in tiffImage.Frames)
        {
           tiffImage.ActiveFrame = tiffFrame;


          
           Color[] pixels = tiffImage.LoadPixels(tiffFrame.Bounds);

           
           var tempImage = Image.Load(stream);

           int width = 500;
           int height = 500;

           width = tempImage.Width;
           height = tempImage.Height;

           
           TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegYCbCr);

         
           tiffOptions.Photometric = TiffPhotometrics.Rgb;

           
           tiffOptions.BitsPerSample = new ushort[] { 8, 8, 8 };

           
           tiffOptions.Source = new FileCreateSource(directory + "\\MultiPages.tif", false);
        }

        // Rotating all pages with 90 degree clockwise.
        tiffImage.Rotate(90);

        // Temporary file name for store the tiff.
        string tempFileName = directory + "\\MultiPagesTestImage.tif";

        using (FileStream temporaryStream = new FileStream(tempFileName, FileMode.Create))
        {
           // Save the TIFF image.
           if (tiffImage.Frames.Length > 0)
              tiffImage.Save(temporaryStream);
        }

     }
  }

}
}

By above code I am able to rotate the tiff document by using JpegOldStyle , but when I am reloading this manipulated document again then it is throwing exception like, “No imaging component suitable to complete this operation was found.” ** [The component cannot be found. (Exception from HRESULT: 0x88982F50)]** , I think this exception related to a failure to read/decode an image file correctly.
where as If I am reloading the original tiff document again then it is being uploaded successfully.

Please fond the snap of manipulated documents property window:

ManipulatedDoc2.png (17.5 KB)
MultiPageDocRotated.png (18.1 KB)

Can you please provide the solution for this issue?

@ankitraman,

We request for your patience and will get back to you as soon as possible. However, I again request you to please provide source Tiff Image that we may use on our end to help you.

@mudassir.fayyaz ,
Thank you for your reply,
Please find the attached source tiff image.

@ankitraman,

I suggest you to please try using following sample code on your end and share with us if you encounter any issue.

 private static void RotateTiffPage()
        {
            // Change the Path of Image file as per requirement          

            string directory = @"d:\isc\workspace\support\tiff";

            int i = 1;
            using (TiffImage tiffImage = (TiffImage)Image.Load(Path.Combine(directory, "img4.tif")))
            {
                foreach (TiffFrame tiffFrame in tiffImage.Frames)
                {
                    tiffImage.ActiveFrame = tiffFrame;

                    if (i / 2 == 0)
                    {
                        tiffFrame.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    }

                    i++;                
                }

                TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegYCbCr);


                tiffOptions.Photometric = TiffPhotometrics.Rgb;


                tiffOptions.BitsPerSample = new ushort[] { 8, 8, 8 };

                tiffImage.Save(Path.Combine(directory, "img4_out.tif"),tiffOptions);

            }
        }

Archive.zip (72.5 KB)

Hi @mudassir.fayyaz ,
Thanks for the solution, now I am able load the manipulated images.
but, one more thing, after manipulation, it increases the size of the document from 900KB to 1848KB,
is there any solution to reduce the size around near to original tiff size?

Also can you please let me know the issue with my demo code,which lead to fail loading the tiff image?

@ankitraman,

It’s good things are fine now on your end.

In this case, I suggest you to please share the source file and generated output file. Actually, we shared you the solution based on our images that I attached too with me response as you had not share any image by then.

@mudassir.fayyaz ,
earlier my tif image was not getting load and this issue is fixed now, but when I am doing rotation of loaded tiff image,I am getting Framework exception while saving time ,I am getting below exception:

    Image export failed.

Type :	Aspose.Imaging.CoreExceptions.ImageSaveException
Message :	Image export failed.
HResult :	0x80131500
Source :	Aspose.Imaging
 at Aspose.Imaging.Image.Save(Stream stream, ImageOptionsBase optionsBase, Rectangle             boundsRectangle)
at Aspose.Imaging.Image.Save(Stream stream, ImageOptionsBase optionsBase)

Caused by:
Type :	Aspose.Imaging.CoreExceptions.FrameworkException
Message :	Cannot allocate so many bytes. Use LoadPartialPixels instead.
HResult :	0x80131500
Source :	Aspose.Imaging
at    .(Object , Int32 )
at    .(Int32 )
at    .(    , Rectangle , Int32 , Int32 , Int32 , Boolean )
at    .(Int32 , Int32 , Int32 , Rectangle )
at    .      (Rectangle )
at    .LoadRawData(Rectangle , RawDataSettings , IPartialRawDataLoader )
at    .LoadPartialArgb32Pixels(Rectangle , IPartialArgb32PixelLoader )
   at Aspose.Imaging.RasterImage..      (Rectangle )
   at    .(    , IList`1 )
   at    .(Rectangle ,     ,  , Int32 , Int32 )
   at Aspose.Imaging.RasterImage.(Rectangle , Int32[] , Boolean , IPartialArgb32PixelLoader    )
   at Aspose.Imaging.RasterImage.(Rectangle , Boolean , IPartialArgb32PixelLoader )
   at Aspose.Imaging.RasterImage.LoadArgb32Pixels(Rectangle rectangle)
   at    ..      (Rectangle )
   at    .(    , IList`1 )
   at    .(Rectangle ,     ,  , Int32 , Int32 )
  at    .​     (TiffStreamReader , Rectangle , IPartialArgb32PixelLoader )
  at    .LoadPartialArgb32Pixels(Rectangle , IPartialArgb32PixelLoader )
   at Aspose.Imaging.RasterImage..      (Rectangle )
  at    .(    , IList`1 )
  at    .(Rectangle ,     ,  , Int32 , Int32 )
  at Aspose.Imaging.RasterImage.(Rectangle , Int32[] , Boolean , IPartialArgb32PixelLoader )
   at Aspose.Imaging.RasterImage.(Rectangle , Boolean , IPartialArgb32PixelLoader )
  at Aspose.Imaging.RasterImage.LoadPartialArgb32Pixels(Rectangle desiredRectangle,  IPartialArgb32PixelLoader pixelLoader)
  at    .(RasterImage , Rectangle , IPartialArgb32PixelLoader )
  at Aspose.Imaging.RasterCachedImage.CacheData()
  at Aspose.Imaging.FileFormats.Tiff.TiffImage.CacheData()
 at Aspose.Imaging.DataStreamSupporter.(Stream )
 at Aspose.Imaging.Image.Save(Stream stream, ImageOptionsBase optionsBase, Rectangle boundsRectangle)

Caused by:
Type :	Aspose.Imaging.CoreExceptions.FrameworkException
Message :	Cannot allocate so many bytes. Use LoadPartialPixels instead.
  HResult :	0x80131500
Source :	Aspose.Imaging
 at Aspose.Imaging.RasterImage..Process(Rectangle , Int32[] , Point , Point )
 at Aspose.Imaging.RasterImage..Process(Rectangle , Int32[] , Point , Point )
 at ​  .Process(Rectangle , Byte[] , Point , Point )
  at    .(Byte[][] )
  at    .(Object , Int32 )

Please find the Tif Image file and exception snap attached.

AsposeFrameWorkException.jpg (68.3 KB)

Kindly help me short it out.

@ankitraman,

Can you please share complete environment details along with version which you are using on your end.

@Adnan.Ahmad ,

Please find details below:

Processor : Intel® Core™ i7 CPU@2.60Hz 2.59Hz
RAM: 16GB
System Type: 64-bit OS x64-based processor

Aspose Version : 17.4.0.0 for .NET

Let me know for any more details.

Kindly let me know the solution for this exception, if possible.

@ankitraman,

You are using a very old version on your end. I suggest you to please try using latest Aspose.Imaging 19.11 on your end and in case issue is still reproduced, please feel free to share with us. I request you to please provide a working solution project as from exception it is not evident which .NET Framework is used on your end.

@mudassir.fayyaz,
Is this Framework exception fixes released in Aspose.Imaging 19.11 ?
Or,
Is there any other way to handle this exception in Aspose.Imaging 17.4.0.0 ?

@mudassir.fayyaz ,
After using Licensed version of Aspose.Imaging 19.11 on my end I am getting below exception:

Type :	System.IO.FileLoadException
Message :	Could not load file or assembly 'Aspose.Imaging, Version=19.11.0.0, Culture=neutral,    PublicKeyToken=716fcg554b201e56' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
HResult :	0x80131040 

Am I missing something here?
Kindly update on it as early, if possible.

@ankitraman,

In this case, I suggest you to please make a new sample project and install Aspose.Imaging for .NET from Nuget. I hope the issue will be rectified.

@mudassir.fayyaz ,
I have latest License for Aspose, and it’s subscription expiryDate is 20200305, but still I am getting below exception:

'The subscription included in this license allows free upgrades until 20 Feb 2019, but this version of the product was released on 05 Nov 2019. Please renew the subscription or use a previous version of the product.'

Please suggest the steps to overcome from this issue as early, if possible.