Converting jpeg to grayscale at 300DPI and 8BIT not working

Hi there,

My product needs to convert a regular jpeg image to a grayscale image at 300DPI/8BIT.
I use Aspose.Imaging v16.12 but code doesn’t work as expect. If I use JpegOption when saving the picture, it outputs a grayscale at 8BIT but not 300DPI; If I use ExifData to set DPI, it outputs a 300DPI with 24BIT RGB, I can’t seem to get both working. I’ve attached my demo app for troubleshooting.

Hi Hui,

Thank you for your inquiry and sharing sample.

This is to update you that we have investigated the issue at our end. Initial investigation shows that there are two scenarios:

  • If we save the resultant image to disk, it works fine. Sample code and sample input file is attached for your reference.
  • If we save the resultant image to stream, it is not working properly.

The issue has been logged into our system with ID IMAGINGNET-2204. Our product team will further look into it and provide feedback. We will update you with feedback via this forum thread.
CODE:

Aspose.Imaging.FileFormats.Jpeg.JpegImage image = new Aspose.Imaging.FileFormats.Jpeg.JpegImage(filename);
Aspose.Imaging.ImageOptions.JpegOptions jOptions = new Aspose.Imaging.ImageOptions.JpegOptions();
jOptions.Quality = 100;
jOptions.ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.Grayscale;
jOptions.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(true);
jOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(300.0, 300.0);

image.Save(@"JPEG_out.jpeg", jOptions);

Do you mean saving to stream NOT working properly? I got confused, if save disk works fine and save to stream works properly why would you log a bug in your system??

Hi Hui,

Yes, you got it right. Saving the converted image to stream is not working properly that was the reason the issue was logged. On the other hand if we save the converted image to disk, this functionality is working fine.

Hi there,

I just tried with your sample code, saving to disk doesn’t work either, the DPI is still 96 not 300.see attached screenshot.

btw I have windows 7 and .net framework 4.5 if these matter

Hi Hui,

Thank you for writing us back.

This is to update you that the information shared by you has been forward to the product team. They will consider it while investigating the issue.

Hi Hui,

This is to update you that our product team has investigated the issue thoroughly. It was found that Windows/GDI shows 96dpi, InfranView shows 300dpi, GIMP shows 72 dpi and you are using GDI to get image resolution. While our product team fixes the issue, you can use the work around given below:

CODE:

public static void ConvertImageToAsposeGreyScale(byte[] sourceImageBytes)
{
byte[] ret;
using (MemoryStream ms = new MemoryStream(sourceImageBytes))
{
using (Aspose.Imaging.FileFormats.Jpeg.JpegImage ji = new Aspose.Imaging.FileFormats.Jpeg.JpegImage(ms))
{
// Load jpeg image, create jpeg options with grayscale 8bit and set resolution to 300
Aspose.Imaging.ImageOptions.JpegOptions jOptions = new Aspose.Imaging.ImageOptions.JpegOptions();
jOptions.Quality = 100;
jOptions.ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.Grayscale;
jOptions.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(true);
jOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(300.0, 300.0);

if (jOptions.ExifData == null)
{
jOptions.ExifData = new Aspose.Imaging.Exif.JpegExifData();
}

using (MemoryStream dst = new MemoryStream())
{
ji.Save(dst, jOptions);
ret = dst.ToArray();
PrintDPI(ret);
}
}
}
}

You can get image bytes as:
string inputFileName = "sample_JPEG.jpeg";
byte[] imageBytes = File.ReadAllBytes(inputFileName);
ConvertImageToAsposeGreyScale(imageBytes);

You can save image to disk as below:
string outputFileName = "sample_JPEG_out.jpeg";
ji.Save(outputFileName, jOptions);

The issues you have found earlier (filed as IMAGINGNET-2204) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by mudassir.fayyaz

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