We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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()

{<o:p></o:p>

Document doc = new Document(@“307_100222_srinivasgoud\in.doc”);<o:p></o:p>

//get shapes collection<o:p></o:p>

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true, false);<o:p></o:p>

int imageIndex = 0;<o:p></o:p>

foreach (Shape shape in shapes)<o:p></o:p>

{<o:p></o:p>

if (shape.HasImage)<o:p></o:p>

{<o:p></o:p>

//generate image name<o:p></o:p>

string imageFileName = string.Format(“image_{0}.{1}”, imageIndex, shape.ImageData.ImageType);<o:p></o:p>

//get image<o:p></o:p>

MemoryStream stream = new MemoryStream(shape.ImageData.ImageBytes);<o:p></o:p>

Image img = Image.FromStream(stream);<o:p></o:p>

//resize image<o:p></o:p>

img = ResizeImage(img, 100, 100);<o:p></o:p>

//save image<o:p></o:p>

img.Save(@“307_100222_srinivasgoud” + imageFileName);<o:p></o:p>

imageIndex++;<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

///

<o:p></o:p>

/// Resize source image to specifed sze<o:p></o:p>

/// <o:p></o:p>

/// Source image<o:p></o:p>

/// Target width<o:p></o:p>

/// Target height<o:p></o:p>

/// Resized image<o:p></o:p>

private Image ResizeImage(Image sourceImage, int targetWidth, int targetHeight)<o:p></o:p>

{<o:p></o:p>

float ratioWidth = (float)sourceImage.Width / targetWidth;<o:p></o:p>

float ratioHeight = (float)sourceImage.Height / targetHeight;<o:p></o:p>

if (ratioWidth > ratioHeight)<o:p></o:p>

targetHeight = (int)(targetHeight * (ratioHeight / ratioWidth));<o:p></o:p>

else<o:p></o:p>

{<o:p></o:p>

if (ratioWidth < ratioHeight)<o:p></o:p>

targetWidth = (int)(targetWidth * (ratioWidth / ratioHeight));<o:p></o:p>

}<o:p></o:p>

Image outputImage = sourceImage.GetThumbnailImage(targetWidth, targetHeight, null, newIntPtr());<o:p></o:p>

return outputImage;<o:p></o:p>

}


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:

http://www.aspose.com/docs/display/wordsnet/About+Shapes+in+Aspose.Words
http://www.aspose.com/docs/display/wordsnet/Retrieving+Sizes+of+Shape+or+DrawingML

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);<o:p></o:p>

foreach (Shape shape in shapes)<o:p></o:p>

{<o:p></o:p>

if (shape.HasImage)<o:p></o:p>

{<o:p></o:p>

MemoryStream stream = new MemoryStream(shape.ImageData.ImageBytes);<o:p></o:p>

Image img = Image.FromStream(stream);<o:p></o:p>

//resize image<o:p></o:p>

img = ResizeImage(img, 100, 100);<o:p></o:p>

shape.ImageData.SetImage(img);<o:p></o:p>

shape.Width = 100;<o:p></o:p>

shape.Height = 100;<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

doc.Save(@“C:\Temp\out.doc”);<o:p></o:p>


private static Image ResizeImage(Image sourceImage, int targetWidth, int targetHeight)<o:p></o:p>

{<o:p></o:p>

float ratioWidth = (float)sourceImage.Width / targetWidth;<o:p></o:p>

float ratioHeight = (float)sourceImage.Height / targetHeight;<o:p></o:p>

if (ratioWidth > ratioHeight)<o:p></o:p>

targetHeight = (int)(targetHeight * (ratioHeight / ratioWidth));<o:p></o:p>

else<o:p></o:p>

{<o:p></o:p>

if (ratioWidth < ratioHeight)<o:p></o:p>

targetWidth = (int)(targetWidth * (ratioWidth / ratioHeight));<o:p></o:p>

}<o:p></o:p>

Image outputImage = sourceImage.GetThumbnailImage(targetWidth, targetHeight, null, new IntPtr());<o:p></o:p>

return outputImage;<o:p></o:p>

}


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.