JPG to TIF conversion (C# .NET)

I note that it seems not possible with Aspose.Imaging to save a JPG color image to TIF CCITTG4 B&W, TiffSaveOptions class not giving the option to specify the good backup settings as compression Type.

This is confirmed by the forum Aspose.Imaging Product family in topic "Save Image to TIFF with compression".

Do you think there it is possible to circumvent the problem by creating a pdf including the jpg image, then converting the pdf to TIFF G4?

Why do simple when you can make it complicated (Shadock proverb)? ...

Hi,


Thank you for your interest in Aspose products.

You are right, at the moment Aspose.Imaging does not support Tiff compression modes. We already have this feature on our road map and we are planning to provide it in some future release of Aspose.Imaging.

Regarding your original question, Aspose.Imaging also does not support loading and conversion of PDF documents to images. By using only Aspose.Imaging component, you will not be able to create PDF and save it to Tiff format. I will check if Aspose.Pdf provide any such feature and will let you know here.

Meanwhile, if possible, please elaborate your scenario.

Hi Xavier,
You are right and thank you for suggesting this. One workaround for converting images to Tiff (with compression) is to add your image to a PDF document and then save the PDF to Tiff. Aspose.Pdf provides four compression modes including CCITT3 and CCITT4.

I am wondering if JPG to TIF is supported yet?

Hi,


Thank you for considering Aspose products.

Yes, JPG to TIFF format conversion is available since v1.5.0 of Aspose.Imaging for .NET. Please check the below source code for your reference.

C#

File.Copy(currentpath + “in.jpg”, currentpath + “copy.jpg”, true);
using (var image = Aspose.Imaging.Image.Load(currentpath + “copy.jpg”))
{
var save = new Aspose.Imaging.SaveOptions.TiffSaveOptions();
image.Save(currentpath + “out.tif”, save);
}

Please feel free to write back in case of further inquiries or comments.
Regards,

I am testing Aspose.Imaging v1.5.0 to convert .jpg files to tiff CCITT G4.

The resulting file is not readable. For example, The Gimp return error messages when loading tiff file:

Line length mismatch at line 21 of strip 0 (got 1921, expected 1920)

Line length mismatch at line 22 of strip 0 (got 1921, expected 1920)

...

To many error messages

I dont found actually any sample in documentation.

See below the options I used:

options.BitsPerSample = ( short)raster.ColorDepth;
            <SPAN style="COLOR: blue">switch</SPAN> (raster.ColorDepth)
            {
            <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #2b91af">RasterRenditionColorDepth</SPAN>.Color1bpp:
                <SPAN style="COLOR: green">//Set TiffCompression</SPAN>
                options.Compression = Aspose.Imaging.FileFormats.Tiff.Enums.<SPAN style="COLOR: #2b91af">TiffCompressions</SPAN>.CcittFax4;
                options.FaxT4Options = Aspose.Imaging.FileFormats.Tiff.Enums.<SPAN style="COLOR: #2b91af">Group3Options</SPAN>.Encoding2D;
                <SPAN style="COLOR: blue">break</SPAN>;

            <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #2b91af">RasterRenditionColorDepth</SPAN>.Color8bpp:
                <SPAN style="COLOR: green">//Set TiffCompression</SPAN>
                options.Compression = Aspose.Imaging.FileFormats.Tiff.Enums.<SPAN style="COLOR: #2b91af">TiffCompressions</SPAN>.Lzw;
                <SPAN style="COLOR: blue">break</SPAN>;

            <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #2b91af">RasterRenditionColorDepth</SPAN>.Color24bpp:
            <SPAN style="COLOR: blue">case</SPAN> <SPAN style="COLOR: #2b91af">RasterRenditionColorDepth</SPAN>.Color32bpp:
            <SPAN style="COLOR: blue">default</SPAN>:
                <SPAN style="COLOR: blue">throw</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">InvalidTransformationException</SPAN>(<SPAN style="COLOR: blue">string</SPAN>.Format(<SPAN style="COLOR: #a31515">"Color depth {0} is not supported for rendition {1}"</SPAN>, raster.ColorDepth.ToString(), raster.GetType().Name));
            }
            
            options.Photometric = Aspose.Imaging.FileFormats.Tiff.Enums.<SPAN style="COLOR: #2b91af">TiffPhotometrics</SPAN>.MinIsBlack;
            options.Orientation = Aspose.Imaging.FileFormats.Tiff.Enums.<SPAN style="COLOR: #2b91af">TiffOrientations</SPAN>.BottomLeft;
            options.BigEndian = <SPAN style="COLOR: blue">false</SPAN>;

            options.Xposition = 0;
            options.Yposition = 0;

            options.Orientation = Aspose.Imaging.FileFormats.Tiff.Enums.<SPAN style="COLOR: #2b91af">TiffOrientations</SPAN>.BottomLeft;

            <SPAN style="COLOR: green">//Set Horizontal Resolution</SPAN>
            options.Xresolution = raster.Hres;

            <SPAN style="COLOR: green">//Set Vertical Resolution</SPAN>
            options.Yresolution = raster.Vres;

            <SPAN style="COLOR: #2b91af">MemoryStream</SPAN> ms = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">MemoryStream</SPAN>();

            docParent.logger.Debug(<SPAN style="COLOR: #a31515">"Transform jpg to TIFF"</SPAN>);

            <SPAN style="COLOR: blue">this</SPAN>.Image2Transform.Save(ms, options);</PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px" dir=ltr><SPAN id=result_box lang=en a="undefined" closure_uid_3mwvmv="90" Sc="null" f="4"><SPAN class=hps closure_uid_3mwvmv="558" Sc="null">(2D is standard for G4, 1D for G3)</SPAN></SPAN></PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px" dir=ltr><SPAN lang=en a="undefined" closure_uid_3mwvmv="90" Sc="null" f="4"><SPAN class=hps closure_uid_3mwvmv="558" Sc="null">I certainly miss something ?</SPAN></SPAN></PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px" dir=ltr><SPAN lang=en a="undefined" closure_uid_3mwvmv="90" Sc="null" f="4"><SPAN class=hps closure_uid_3mwvmv="558" Sc="null">Moreover </SPAN><SPAN class=hps closure_uid_3mwvmv="559" Sc="null">there are now</SPAN> <SPAN class=hps closure_uid_3mwvmv="560" Sc="null">tiff G4 files</SPAN> <SPAN class=hps closure_uid_3mwvmv="561" Sc="null">which are no longer</SPAN> <SPAN class=hps closure_uid_3mwvmv="562" Sc="null">loadable but</SPAN><SPAN class=hps closure_uid_3mwvmv="563" Sc="null"> files</SPAN> <SPAN class=hps closure_uid_3mwvmv="564" Sc="null">present no problem</SPAN><SPAN closure_uid_3mwvmv="565" Sc="null">.</SPAN></SPAN></PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px" dir=ltr><SPAN lang=en a="undefined" closure_uid_3mwvmv="90" Sc="null" f="4"><SPAN closure_uid_3mwvmv="565" Sc="null"></SPAN></SPAN> </PRE>

Hi Xavier,


Thank you for considering Aspose products.

Please find below source code to convert Jpeg image to Tiff with CCITT4 compression.

C#

File.Copy(currentpath + “input.jpg”, currentpath + “copy.jpg”, true);
using (var image = Aspose.Imaging.Image.Load(currentpath + “copy.jpg”))
{
var save = new Aspose.Imaging.SaveOptions.TiffSaveOptions();
save.Compression = Aspose.Imaging.FileFormats.Tiff.Enums.TiffCompressions.CcittFax4;
save.Photometric = Aspose.Imaging.FileFormats.Tiff.Enums.TiffPhotometrics.MinIsBlack;
save.BitsPerSample = 1;
save.SamplesPerPixel = 1;
image.Save(currentpath + “out.tif”, save);
}
Please note, in above code snippet, I have made a copy of original image before further processing. The reason for this extra step is: current implementation of Aspose.Imaging alters the original image. We are currently working hard to fix this issue with the upcoming release.

In case you still find any problems while converting Jpeg to Tiff, please share your sample with us.

Regarding the last part of your inquiry, if you are having difficulties while loading CCITT4 compressed images, please share the problematic sample and your code snippet for your review.

Regards,

Actually we abandon the usage of aspose. The exact application of your code doesnt work.

If we do something much simpler, with the last realease of Imaging dll with no compression and default options with jpg in attachement as source file, we obtain tif in attachement as destination file. It is easy to see that many tools (visualisation in windows, paint.net etc) canot open the resulting tif file and with the gimp, the ending of image is not displayed ( black band at the end) even the size seems good.

Image2Transform = Aspose.Imaging.Image.Load("IAExplorer.JPG");
Aspose.Imaging.ImageOptions.TiffOptions options = new Aspose.Imaging.ImageOptions.TiffOptions();
options.Photometric = Aspose.Imaging.FileFormats.Tiff.Enums.TiffPhotometrics.MinIsBlack;
Image2Transform.Save("[.JPG to .TIF] IAExplorer_0.tif", options);

Hi Xavier,


Thank you for writing back.

I have tested your sample and I am able to produce the correct results with Aspose.Imaging for .NET v1.6.0. Please note, if you are converting the Jpeg image to Tiff without any compression then you can simply pass a new instance of TiffOptions to Image.Save method. Below is the sample code and attached is my output that can be opened using the Windows Photo Viewer.

C#

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(“IAExplorer.JPG”))
{
var saveOptions = new Aspose.Imaging.ImageOptions.TiffOptions();
image.Save(“output.tiff”, saveOptions);
}

If I have misunderstood your inquiry then please elaborate your question so I may assist you appropriately.

Regards,

We are also facing the same issue for much simpler task i.e. JPEG to TIFF conversion doesn't work on Windows XP.

Eventhough the converted file shows .tiff extension but when opened in MS Paint it gives an error. Also, the tiff generated is not recognised by other Utilities.

Hi Hitesh,


Thanks for your inquiry. Please check my reply here against your other query. Moreover, as I already mentioned XP is not supporting all the extensions of tiff image. You can use some other image utilities for the purpose e.g. Irfanview. Can you please mention utility not supporting resultant tiff image, other than XP native utilities? So we will test it and will provide you more information.

Best Regards,