Hi there,
Using latest version (25.9) for .NET.
We want to use Image.GetOriginalOptions() when changing resolution so we can keep all other aspects of the image (compression, color depth, etc.). However, we are not able to change Vertical resolution unless we use a new TiffOptions instead (which is not ideal).
Try the code below with any tiff image that doesn’t already have 150 DPI:
var outputPathBadResolution = inputPath + "_bad.tif";
var outputPathGoodResolution = inputPath + "_good.tif";
using (var image = Image.Load(inputPath))
{
var exportOptions = (TiffOptions)image.GetOriginalOptions();
exportOptions.ResolutionSettings = new ResolutionSetting(150, 150);
// this only changes one of the resolutions
image.Save(outputPathBadResolution, exportOptions);
// create new TiffOptions (not what we want because we don't want to change color depth or compression)
exportOptions = new TiffOptions(TiffExpectedFormat.TiffLzwRgb)
{
ResolutionSettings = new ResolutionSetting(150, 150)
};
// this ensures both horizontal and vertical are both 150
image.Save(outputPathGoodResolution, exportOptions);
}
using (var image = (TiffImage)Aspose.Imaging.Image.Load(outputPathBadResolution))
{
Console.WriteLine($"Using GetOriginalOptions() - Vertical: {image.VerticalResolution}; Horizontal: {image.HorizontalResolution};");
}
using (var image = (TiffImage)Aspose.Imaging.Image.Load(outputPathGoodResolution))
{
Console.WriteLine($"Using new TiffOptions() - Vertical: {image.VerticalResolution}; Horizontal: {image.HorizontalResolution};");
}
You will see this output (Vertical resolution will be what it was before, in my case 92):
Using GetOriginalOptions() - Vertical: 92; Horizontal: 150;
Using new TiffOptions() - Vertical: 150; Horizontal: 150;
Is there a property I need to change to get this to work properly?
Thanks,
Mark
@markmanley.adlib
Hi Mark,
It seems you’re encountering an issue with changing the vertical resolution of TIFF images using Image.GetOriginalOptions()
. The method you’re using only updates the horizontal resolution. To change the vertical resolution, you need to create a new TiffOptions
instance.
To achieve your goal of maintaining all other image properties while changing both horizontal and vertical resolutions, please follow these steps:
- Load the image and retrieve the original options.
- Create a new
TiffOptions
instance with the desired resolution settings.
- Copy the properties from the original options to the new options, ensuring you maintain compression, color depth, etc.
- Save the image using the new options.
Here’s a code snippet to illustrate this:
var outputPath = inputPath + "_updated.tif";
using (var image = Image.Load(inputPath))
{
var originalOptions = (TiffOptions)image.GetOriginalOptions();
// Create new TiffOptions and set the desired resolution
var newOptions = new TiffOptions(TiffExpectedFormat.TiffLzwRgb)
{
ResolutionSettings = new ResolutionSetting(150, 150),
Compression = originalOptions.Compression,
BitsPerSample = originalOptions.BitsPerSample,
Photometric = originalOptions.Photometric
};
// Save the image with the new options
image.Save(outputPath, newOptions);
}
This approach will effectively change both the horizontal and vertical resolutions while keeping the other properties intact.
Please implement this solution and let me know if you have any further questions or need additional assistance!
Does it matter what setting I use for TiffExpectedFormat if I’m always overwriting Compression, etc.?
Also, what about copying other rare properties like Predictor, alpha, transparency, etc.? What else would I need to copy?
@markmanley.adlib Hello! We will investigate this issue and get back to you as soon as possible.
Please note: I have tried not using GetOriginalOptions() directly and copying it, but that is causing multiple other issues, so it is not an option. I should be able to set GetOriginalOptions() but then override/change things like resolution.
Edit: This might be a clue: after I set 150 for both and check while on a breakpoint after setting it, I still see 92 (but this only happens after using GetOriginalOptions()):
image.png (13.6 KB)
Thanks!
Looks like we found the issue. When using GetOriginalOptions, if we want to change the resolution, we also have to do the following along with ResolutionSettings. To me, setting ResolutionSettings should do both, but it’s not the case:
originalOptions.Xresolution = new TiffRational(150, 1);
originalOptions.Yresolution = new TiffRational(150, 1);
Maybe this can be updated somewhere in your online tutorials? I hope nothing else will surprise us when using GetOriginalOptions and changing things after.
Do you recommend we do both?
new ResolutionSetting(150, 150)
An example of another thing we had to change if switching compression: if the original compression was LZW and it had Predictor set and we switch to a different compression, we had to set Predictor to None.
Hello, @markmanley.adlib ,
We are reviewing your request. You will be answered shortly!