This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Can't Change Image DPIs

Hello,

I have an image with 72dpi and I want to resize it and change the dpi's (ex: to 300dpi), however the image is saved always with 72dpi.

Here's the code snippet:

public byte[] Resize(byte[] media, int width, int height, float PPI)
{
	using (var imageStream = new MemoryStream(media))
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = ImageReader.Create(imageStream))
	using (var bitmap = imageReader.Frames[0].GetBitmap())
	{
		var jpegSettings = new JpegSettings {Quality = 100};

		#region metadata

		if (imageReader.AdobeResources != null)
		{
			jpegSettings.AdobeResources = imageReader.AdobeResources;
		}

		if (imageReader.Exif != null)
		{
			jpegSettings.Exif = imageReader.Exif;
		}

		if (imageReader.Iptc != null)
		{
			jpegSettings.Iptc = imageReader.Iptc;
		}

		if (imageReader.Xmp != null)
		{
			jpegSettings.Xmp = imageReader.Xmp;
		}
		#endregion
		
		bitmap.Transforms.Resize(width, height);

		bitmap.DpiX = PPI;
		bitmap.DpiY = PPI;


		bitmap.Save(exportedImageStream, jpegSettings);

		return exportedImageStream.GetBuffer();
	}
}




Thank you,

Kind regards
Which way do you check the DPI values?

I have run your code and the resolution was successfully changed.

using Aurigma.GraphicsMill.Codecs;
using System;
using System.IO;

namespace Forum_13976
{
    public class Class1
    {
		public static void Main()
		{
			var inBytes = File.ReadAllBytes("../../../test.jpg"); 
			var outBytes = new Class1().Resize(inBytes, 600, 400, 200f);
			File.WriteAllBytes("../../../out.jpg", outBytes);
		}

		public byte[] Resize(byte[] media, int width, int height, float PPI)
		{
			using (var imageStream = new MemoryStream(media))
			using (var exportedImageStream = new MemoryStream())
			using (var imageReader = ImageReader.Create(imageStream))
			using (var bitmap = imageReader.Frames[0].GetBitmap())
			{
				var jpegSettings = new JpegSettings {Quality = 100};
 
				#region metadata
 
				if (imageReader.AdobeResources != null)
				{
					jpegSettings.AdobeResources = imageReader.AdobeResources;
				}
 
				if (imageReader.Exif != null)
				{
					jpegSettings.Exif = imageReader.Exif;
				}
 
				if (imageReader.Iptc != null)
				{
					jpegSettings.Iptc = imageReader.Iptc;
				}
 
				if (imageReader.Xmp != null)
				{
					jpegSettings.Xmp = imageReader.Xmp;
				}
				#endregion
         
				bitmap.Transforms.Resize(width, height);
 
				bitmap.DpiX = PPI;
				bitmap.DpiY = PPI;
 
 
				bitmap.Save(exportedImageStream, jpegSettings);
 
				return exportedImageStream.GetBuffer();
			}
		}
    }
}

Attachments

Hi,

static void Main(string[] args)
{
	const string path = @"D:\resource.jpg";
	const string pathOutput = @"D:\out.jpg";

	var file = File.ReadAllBytes(path);

	var imgProvider = new ImageMediaItemProviderService();
	var mediaExported = imgProvider.ResizeImage(file, MediaContentType.Jpeg, 600, 400, 300f);

	File.WriteAllBytes(pathOutput, mediaExported);
}


I used the same image as you, and tried to change the dpi to 300, and the dpi haven't changed (see picture attached).


Kind regards
I have successfully reproduced the issue.

The problem occurs when a JPEG image contains EXIF metadata and resolution specified in the EXIF doesn't match resolution stored in the JPEG header.

So to fix the problem you should update the XResolution and YResolution EXIF tags:

using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.Codecs;
using System;
using System.IO;

namespace Forum_13976
{
    public class Class1
    {
		public static void Main()
		{
			var inBytes = File.ReadAllBytes("../../../resource.jpg"); 
			var outBytes = new Class1().Resize(inBytes, 600, 400, 300f);
			File.WriteAllBytes("../../../out.jpg", outBytes);
		}

		public byte[] Resize(byte[] media, int width, int height, float PPI)
		{
			using (var imageStream = new MemoryStream(media))
			using (var exportedImageStream = new MemoryStream())
			using (var imageReader = ImageReader.Create(imageStream))
			using (var bitmap = imageReader.Frames[0].GetBitmap())
			{
				var jpegSettings = new JpegSettings {Quality = 100};
 
				#region metadata
 
				if (imageReader.AdobeResources != null)
				{
					jpegSettings.AdobeResources = imageReader.AdobeResources;
				}
 
				if (imageReader.Exif != null)
				{
					jpegSettings.Exif = imageReader.Exif;

					var r = new UnsignedRational((uint)(PPI * 10000f), 10000);

					jpegSettings.Exif[ExifDictionary.XResolution] = r;
					jpegSettings.Exif[ExifDictionary.YResolution] = r;
				}
 
				if (imageReader.Iptc != null)
				{
					jpegSettings.Iptc = imageReader.Iptc;
				}
 
				if (imageReader.Xmp != null)
				{
					jpegSettings.Xmp = imageReader.Xmp;
				}
				#endregion
         
				bitmap.Transforms.Resize(width, height);
 
				bitmap.DpiX = PPI;
				bitmap.DpiY = PPI; 

				bitmap.Save(exportedImageStream, jpegSettings);
 
				return exportedImageStream.GetBuffer();
			}
		}
    }
}


We will fix the problem in the further releases (> 8.0.37) of Graphics Mill.
Thank you! 🙂
The recently released version 8.1.2 includes the fix of the problem.