Reduce All Images Dimensions by percentage (Keep Aspect Ratio) in a WORD document

Hi

I have Aspose Total .NET and Word v11.8

I have a word document with lots of images.

  1. I want to open a Word document in Aspose word.
  2. Then reduce EVERY image’s dimensions by a percentage while keeping aspect ratio.
  3. Save the document onto itself again.

Can you help?

Hi Nitin,

Thanks for your inquiry. Please try using the following code snippet to be able to resize images in a Document:

public void TestResizeImages_100222()
{
    Document doc = new Document(@"307_100222_srinivasgoud\in.doc");
    // get shapes collection
    NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true, false);
    int imageIndex = 0;
    foreach (Shape shape in shapes)
    {
        if (shape.HasImage)
        {
            // generate image name
            string imageFileName = string.Format("image_{0}.{1}", imageIndex, shape.ImageData.ImageType);
            // get image
            MemoryStream stream = new MemoryStream(shape.ImageData.ImageBytes);
            Image img = Image.FromStream(stream);
            // resize image
            img = ResizeImage(img, 100, 100);
            // save image
            img.Save(@"307_100222_srinivasgoud" + imageFileName);
            imageIndex++;
        }
    }
}

/// 
/// Resize source image to specifed sze
/// 
/// Source image
/// Target width
/// Target height
/// Resized image
private Image ResizeImage(Image sourceImage, int targetWidth, int targetHeight)
{
    float ratioWidth = (float)sourceImage.Width / targetWidth;
    float ratioHeight = (float)sourceImage.Height / targetHeight;
    if (ratioWidth > ratioHeight)
        targetHeight = (int)(targetHeight * (ratioHeight / ratioWidth));
    else
    {
        if (ratioWidth < ratioHeight)
            targetWidth = (int)(targetWidth * (ratioWidth / ratioHeight));
    }
    Image outputImage = sourceImage.GetThumbnailImage(targetWidth, targetHeight, null, newIntPtr());
    return outputImage;
}

Also, please note that Images are represented by either Shape or by DrawingML nodes in Aspose.Words’ DOM. I would suggest you please read the following articles:

https://docs.aspose.com/words/net/working-with-graphic-elements/
https://docs.aspose.com/words/net/working-with-images/

I hope, this will help.

Best Regards,

Thank you, I will try this code.

I do not want to save the image out to a file.
I just want to resize the image inside the document.
so for example:
I have doc1.docx with an image of size H:1000 x W: 800
I want the final result to be
doc1.docx with the same image but now size H:500 x W:400
So i just want to end up with the same doc1.docx file but now with the image inside the document reduced by 50%.

Hi Nitin,

Thanks for your inquiry. Please try using the following code snippet to be able to resize images in your Document:

Document doc = new Document(@"C:\Temp\in.doc");
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in shapes)
{
    if (shape.HasImage)
    {
        MemoryStream stream = new MemoryStream(shape.ImageData.ImageBytes);
        Image img = Image.FromStream(stream);
        // resize image
        img = ResizeImage(img, 100, 100);
        shape.ImageData.SetImage(img);
        shape.Width = 100;
        shape.Height = 100;
    }
}
doc.Save(@"C:\Temp\out.doc");
private static Image ResizeImage(Image sourceImage, int targetWidth, int targetHeight)
{
    float ratioWidth = (float)sourceImage.Width / targetWidth;
    float ratioHeight = (float)sourceImage.Height / targetHeight;
    if (ratioWidth > ratioHeight)
        targetHeight = (int)(targetHeight * (ratioHeight / ratioWidth));
    else
    {
        if (ratioWidth < ratioHeight)
            targetWidth = (int)(targetWidth * (ratioWidth / ratioHeight));
    }
    Image outputImage = sourceImage.GetThumbnailImage(targetWidth, targetHeight, null, new IntPtr());
    return outputImage;
}

Best Regards,

HI Awaisv,
Thank you - I will tes the code.

Hi Nitin,

Sure, please let us know any time you have any further queries. We’re always glad to help you.

Best Regards,

yes it works very well. I just tweaked it a little.