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
|
|
|
|
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): |
Best regards, Fedor Skvortsov
|
|
|
|
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):
|
|
|
|
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
|
1 user thanked Fedor for this useful post.
|
|
|
Rank: Member
Groups: Guest
Joined: 1/12/2016(UTC) Posts: 23
Thanks: 8 times
|
|
|
|
|
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
|
1 user thanked Fedor for this useful post.
|
|
|
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.