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

Notification

Icon
Error

Options
Go to last post Go to first unread
bma  
#1 Posted : Tuesday, January 12, 2016 5:31:13 AM(UTC)
bma

Rank: Member

Groups: Guest
Joined: 1/12/2016(UTC)
Posts: 23

Thanks: 8 times
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:

Code:
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

Edited by user Tuesday, January 12, 2016 7:21:23 AM(UTC)  | Reason: Not specified

Fedor  
#2 Posted : Tuesday, January 12, 2016 9:30:06 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Guest
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
Which way do you check the DPI values?

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

Code:
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();
			}
		}
    }
}
Fedor attached the following image(s):
out.jpg
prop.png
Best regards,

Fedor Skvortsov

bma  
#3 Posted : Tuesday, January 12, 2016 10:19:49 AM(UTC)
bma

Rank: Member

Groups: Guest
Joined: 1/12/2016(UTC)
Posts: 23

Thanks: 8 times
Hi,

Code:
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

bma attached the following image(s):
dpi.png
resource.jpg
Fedor  
#4 Posted : Tuesday, January 12, 2016 11:04:47 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Guest
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
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:

Code:
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.

Edited by user Tuesday, January 12, 2016 11:06:07 AM(UTC)  | Reason: Not specified

Best regards,

Fedor Skvortsov

thanks 1 user thanked Fedor for this useful post.
bma on 1/12/2016(UTC)
bma  
#5 Posted : Wednesday, January 13, 2016 10:03:55 AM(UTC)
bma

Rank: Member

Groups: Guest
Joined: 1/12/2016(UTC)
Posts: 23

Thanks: 8 times
Thank you! :)
Fedor  
#6 Posted : Wednesday, January 27, 2016 12:58:22 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Guest
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
The recently released version 8.1.2 includes the fix of the problem.
Best regards,

Fedor Skvortsov

thanks 1 user thanked Fedor for this useful post.
bma on 1/27/2016(UTC)
Users browsing this topic
Guest (3)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.