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,
{<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>
///
/// 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>
}
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,
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>
}
HI Awaisv,
Thank you - I will tes the code.
Hi Nitin,
yes it works very well. I just tweaked it a little.