Detecting resolution of images / Up sampling images in Aspose.Words

I need to check the resolution of images in a word document. The requirement is to have a minimum DPI of 290 for all images.
I am using the below code for the same. (assuming for now that all shapes are images)

foreach (Shape image in doc.GetChildNodes(NodeType.Shape, true))
{
    double imageResolution = image.ImageData.ImageSize.HorizontalResolution;
}

I have attached the document that I am validating.
For both the images the resolution is 220DPI.
After validation I need to upsample the resolution. I am using the below code:

foreach (Shape image in doc.GetChildNodes(NodeType.Shape, true))
{
    if (image.ImageData.ImageSize.HorizontalResolution < expectedDPI && image.HasImage)
    {
        using (Bitmap bitmapImage = new Bitmap(image.ImageData.ToImage()))
        {
            bitmapImage.SetResolution(290, 290);
            MemoryStream stream = new MemoryStream();
            bitmapImage.Save(stream, GetImageType(image.ImageData.ImageType.ToString()));
            image.ImageData.SetImage(stream);
        }
    }
}

After upsampling I convert the word to pdf using the below code.

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.EmbedFullFonts = true;
saveOptions.DownsampleOptions.DownsampleImages = false;
saveOptions.UseHighQualityRendering = true;
saveOptions.ColorMode = ColorMode.Grayscale;
saveOptions.ImageCompression = PdfImageCompression.Jpeg;
saveOptions.JpegQuality = 100;
saveOptions.ImageColorSpaceExportMode = bPdfImageColorSpaceExportMode.SimpleCmyk;
saveOptions.SaveFormat = SaveFormat.Pdf;
MemoryStream stream=new MemoryStream();
_sourceDocument.Save(stream, saveOptions);

In the pdf I am getting different resolutions (used acrobat pro)
Image 1 – 220 DPI
Image 2 – 134 DPI
We can see that the upsample code didn’t work. But then both the images should show a DPI of 220.
I verified that acrobat pro is giving correct results using the formula – DPI = width in pixels/width in inches.
I came to a conclusion that image.ImageData.ImageSize.HorizontalResolution is giving wrong resolution. I think it is not considering the fact that the image 2 has been resized. (On resize the resolution should change right.)
Also is there a way to up sample images and then convert to pdf?
I saved the image in the up sampled document to local folder and checked the properties to find that the resolution is 290 DPI.

Hi Rohith,
I always get the correct resolution at my end. You can try opening the output document in MS Word and saving the images from the document to local folder. As I can see both of the images in your output document are updated and are showing 290 DPI if we open the document in MS Word and save the images in local folder. You can also open your output Word document and check the image size of both images (it will show updated value i.e. 290 DPI).
We are further investigating the up sampled images in PDF issue and will update you soon.
Best Regards,

Hi Ijaz

Thanks for your reply.
If I save the images to a local folder, and check the DPI, it shows 290 DPI which is fine.
I was getting the same results earlier too. I think if we set the resolution of the bitmap, its setting for the original image. But its not setting for the image in the document?
That is why after converting to pdf it shows 220 and 134 DPI respectively for the 2 images.

Same is the issue with getting the resolution. If you put a image without resizing, it will give correct results. But if you resize the image by making it bigger, then the code image.ImageData.ImageSize.HorizontalResolution gives the same resolution (which is the original image resolution). Actually after making the image bigger it should show a less resolution.

Rohith

Hi Rohith,
Thanks for the additional information. We will further investigate this issue and let you know.
Best Regards,

Hi Rohith,
I did not find any evidence where MS Word changes the resolution of the images. You can resize the shape which contains image but actual dimensions of the image remain the same. You can test it by resizing an image in MS Word and then right clicking on the image and saving it as an image file. Upon checking the details of image, you will find that nothing has changed for resized image.
Same is the case with Aspose.Words. As a workaround, you can use Aspose.Imaging or System.Drawing libraries to change the dimensions of image and add as a new images instead of updating existing one.
Best Regards,