Image.Save loosing original resolution (DPI)

I’m trying to convert a lossless TIFF (LZW compression) into a JPEG. The original TIFF image has a horizontal and vertical resolution (DPI) of 300. The resulting JPEG always has a horizontal and vertical resolution of 96. I am using the following code:


using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@“C:\sample.tiff”))
{
    JpegOptions jpegOptions = new JpegOptions
{
Quality = 100
};
image.Save(@“C:\output.jpeg”, jpegOptions);
}

I do not see any properties on the JpegOptions object regarding resolution so I assumed that the original resolution would be preserved.  Am I missing something?

Hi Kerby,


Thank you for contacting Aspose support.

You may use Aspose.Imaging APIs as follow to convert Tiff images to Jpeg while setting resolution of your choice. Please note, the following code requires setting a valid license before calling it because Pixel Level Manipulation is not available in evaluation mode.

C#

//Load image to be converted in an object of RasterImage
using (var image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(myDir + “sample.tiff”))
{
//Create an instance of JpegOptions for resultant Jpeg file
var JpgCreateOptions = new Aspose.Imaging.ImageOptions.JpegOptions();
//Set the Quality
JpgCreateOptions.Quality = 70;
//Set the file source of resultant Jpeg. Last parameter denotes isTemporal
JpgCreateOptions.Source = new FileCreateSource(myDir + “result.jpeg”, false);
//Create a new JpegImage using JpegOptions and size in Width and Height of source image
using (var JpgImage = (Aspose.Imaging.FileFormats.Jpeg.JpegImage)Aspose.Imaging.Image.Create(JpgCreateOptions, image.Width, image.Height))
{
//Set Resolution
JpgImage.HorizontalResolution = 300;
JpgImage.VerticalResolution = 300;
//Load all Pixels (Bitmap information) of source image and save them on Jpeg
JpgImage.SavePixels(JpgImage.Bounds, image.LoadPixels(image.Bounds));
//Save the Jpeg
JpgImage.Save();
}
}


Please feel free to write back.

When I try to set JpgImage.HorizontalResolution = 300; I get a FrameworkException that says “Use Jfif or ExifData for storing resolution.”

Hi Kerby,


Thank you for writing back.

Please share your sample file so we could evaluate the problem and log appropriate ticket if required.

Attached is my sample code. The input TIFF image is in the form of a byte array.

Hi Kerby,


We have already evaluated your presented scenario with a couple of sample Tiff images of our own, and we are unable to see the exception as mentioned in your recent post. Therefore we requested to your please provide the problematic sample image for our review, as we believe the said problem is caused by the input image it self. You may save the byte array to file without using Aspose.Imaging APIs and attach the sample here for further investigation.

Please note, we may not be able to move forward towards the correction of the said issue (if identified as a bug on the part of Aspose.Imaging APIs) unless we replicate the issue on our end.

Thank you for your understanding and cooperation.

Attached is my sample TIFF

Hi Kerby,


Thank you for providing the sample image.

We have thoroughly evaluated your presented scenario on our end, and have identified a bug on the part of Aspose.Imaging for .NET with latest releases v2.3.0 and v2.3.1. A ticket has been logged in our bug tracking system to probe further into this matter, and to provide a fix with our next major release v2.4.0. The ticket Id for your future reference is IMAGING-34070.

Please note, starting from v2.3.0, we have changed the implementation of JpegImage class to handle EXIF information including the resolution. Once the aforesaid bug is fixed, you will be able to set the Jpeg resolution using the following piece of code.

C#

//Create an instance of JpegOptions for resultant Jpeg file
var JpgCreateOptions = new Aspose.Imaging.ImageOptions.JpegOptions();
//Set the Quality
JpgCreateOptions.Quality = 70;
//Set the file source of resultant Jpeg. Last parameter denotes isTemporal
JpgCreateOptions.Source = new FileCreateSource(myDir + “result.jpeg”, false);
//Create a new JpegImage using JpegOptions and size in Width and Height of source image
using (var JpgImage = (Aspose.Imaging.FileFormats.Jpeg.JpegImage)Aspose.Imaging.Image.Create(JpgCreateOptions, image.Width, image.Height))
{
//Set Resolution
if (JpgImage.ExifData == null)
{
JpgImage.ExifData = new Aspose.Imaging.Exif.JpegExifData();
}
JpgImage.ExifData.ResolutionUnit = Aspose.Imaging.Exif.Enums.ExifUnit.Inch;
JpgImage.ExifData.XResolution = new TiffRational(300);
JpgImage.ExifData.YResolution = new TiffRational(300);

//Load all Pixels (Bitmap information) of source image and save them on Jpeg
// JpgImage.SavePixels(JpgImage.Bounds, image.LoadPixels(image.Bounds));

//Save the Jpeg
JpgImage.Save();
}


We would request you to please download and use Aspose.Imaging for .NET 2.2.0 with the code snippet provided here till the time we sort out the problem mentioned above. Please note, while using v2.2.0, you will not experience the Framework exception "Use Jfif or ExifData for storing resolution." because these modifications were introduced with v2.3.0.

Please feel free to write back in case you have further questions or concerns.

The issues you have found earlier (filed as IMAGING-34070) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

The issues you have found earlier (filed as ) have been fixed in this Aspose.Words for JasperReports 18.3 update.