Loosing DPI after saving a jpg image

Hello,

I have a created a function to resize an image. But after saving the new image, the DPI is 96 instead of the original 300. So the quality is not what we except. I try to set in code, but in doesn’t seem to work or am I doing something wrong? (When i do the same manually in Microsoft Paint, the quality is much better).

I am using the following code:

public Task ResizeImageAsposeExample(string sourceFile, string destFileName, int width, int height)
{
using (Image image = Image.Load(sourceFile))
{
var ressetting = new ResolutionSetting();
ressetting.HorizontalResolution = 300;
ressetting.VerticalResolution = 300;

            var jpegoptions = new Aspose.Imaging.ImageOptions.JpegOptions();
            jpegoptions.ResolutionSettings = ressetting;
            jpegoptions.Quality = 100;
            image.Resize(width, height, ResizeType.LanczosResample);

            if (!Directory.Exists(Path.GetDirectoryName(destFileName)))
                Directory.CreateDirectory(Path.GetDirectoryName(destFileName));
            string ext = Path.GetExtension(sourceFile);
            if (string.Equals(ext, ".jpg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".jpeg", StringComparison.OrdinalIgnoreCase))
                image.Save(destFileName, jpegoptions);
           
        }
        return Task.FromResult(true);
    }
Hi Frank,

Thank you for your inquiry.

Please note that resizing image while setting resolution of your choice involves pixel level manipulation. You have to first set a valid license in the code because Pixel Level Manipulation is not available in evaluation mode. Following is the sample code snippet for your reference.
//Load image to be converted in an object of RasterImage
using (var image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(sourceFile))
{
//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(destFileName, 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();
}
}

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

Thanks for the reply!







Unfortunatly the code you send was not working, but I solved it myself. I was reading another post with a similair problem.



These 2 lines are not working:







JpgImage.HorizontalResolution = 300; JpgImage.VerticalResolution = 300;







I replaced it by the following code:







if (JpgImage.ExifData == null)

{

JpgImage.ExifData = new Aspose.Imaging.Exif.JpegExifData();

}

JpgImage.ExifData.ResolutionUnit = Aspose.Imaging.Exif.Enums.ExifUnit.Inch;

JpgImage.ExifData.XResolution = new Aspose.Imaging.FileFormats.Tiff.TiffRational(300);

JpgImage.ExifData.YResolution = new Aspose.Imaging.FileFormats.Tiff.TiffRational(300);







Now the code is running, but the result is not wat I expect.







The case that I have is to resize a High resolution image without loosing to much quality. I compare the result with e.g.



Microsoft Paint.



I’m not convinced that pixels are loosing in the resize proces. Look at the following code (resize line is



commented):







using (var image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(sourceFile))

{

//Create an instance of JpegOptions for resultant Jpeg file

var JpgCreateOptions = new Aspose.Imaging.ImageOptions.JpegOptions();

//Set the Quality

JpgCreateOptions.Quality = 100;

//Set the file source of resultant Jpeg. Last parameter denotes isTemporal

JpgCreateOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(destFileName, 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;

if (JpgImage.ExifData == null)

{

JpgImage.ExifData = new Aspose.Imaging.Exif.JpegExifData();

}

JpgImage.ExifData.ResolutionUnit = Aspose.Imaging.Exif.Enums.ExifUnit.Inch;

JpgImage.ExifData.XResolution = new Aspose.Imaging.FileFormats.Tiff.TiffRational(300);

JpgImage.ExifData.YResolution = new Aspose.Imaging.FileFormats.Tiff.TiffRational(300);



//Load all Pixels (Bitmap information) of source image and save them on Jpeg

JpgImage.SavePixels(JpgImage.Bounds, image.LoadPixels(image.Bounds));

JpgImage.Resize(width, height, ResizeType.LanczosResample);



//Save the Jpeg

JpgImage.Save();

}

}



This will just generate a new image with the same size as the Original, only the quality is poor. If you take a look at the



source file, the size is 3MB. The output file is just: 543 kB. So why do I loose so much quality?







The original size of the image I test with is 2215x1151. I want to resize it back to 700x363. If I do it in Microsoft Paint, the file is still 1.8MB and the quality is very high.







If enable the resize line in the code sample, the output file is just 45,5 kb.







I will attach all the files I test with. The source file and 3 output files. One of them I resized it in Micorsoft Paint manually. So then you can see the difference by yourself.



Can you help my with this case please? Is Aspose able the render HIGH quality images at all?







Thanks in advance

Hi Frank,

Thank you for writing us back.

Please note that we have used Progressive compression mode to generate the output. It was noticed that the output is same as MS Paint but with significant change in file size. Sample code snippet is given below for your reference. Output of this code snippet has also been attached.

string sourceFile = @"C:\loosing-dpi-after-saving\04213C+source.jpg";
string destFileName = @"C:\loosing-dpi-after-saving\04213C+source__output.jpg";
using (var image = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Load(sourceFile))
{
//Create an instance of JpegOptions for resultant Jpeg file
var JpgCreateOptions = new Aspose.Imaging.ImageOptions.JpegOptions();
//Set the Quality
JpgCreateOptions.Quality = 100;
JpgCreateOptions.CompressionType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode.Progressive;
//Set the file source of resultant Jpeg. Last parameter denotes isTemporal
JpgCreateOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(destFileName, 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;
if (JpgImage.ExifData == null)
{
JpgImage.ExifData = new Aspose.Imaging.Exif.JpegExifData();
}
JpgImage.ExifData.ResolutionUnit = Aspose.Imaging.Exif.Enums.ExifUnit.Inch;
JpgImage.ExifData.XResolution = new Aspose.Imaging.FileFormats.Tiff.TiffRational((uint)image.Width);
JpgImage.ExifData.YResolution = new Aspose.Imaging.FileFormats.Tiff.TiffRational((uint)image.Height);
//Load all Pixels (Bitmap information) of source image and save them on Jpeg
JpgImage.SavePixels(JpgImage.Bounds, image.LoadPixels(image.Bounds));
//JpgImage.Resize(width, height, ResizeType.LanczosResample);
//Save the Jpeg
JpgImage.Save();
}
}

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