Free Support Forum - aspose.com

Changing the image resolution/size while extracting the image from word

HI,



Iam extracting the images from the word document to a folder.i want to stream line all the image size/Resolution to a fixed size.



iam using follwing code

case NodeType.Shape:

{

Shape shape = (Shape)node;

if (shape.HasImage)

{

shape.ImageData.Save(“image.jpg”);

ImageIndex++;

}

}

Hi

Thank you for your interest in Aspose products. You can try to use the following code to achieve this.

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, new IntPtr());

return outputImage;

}

I hope that this will help you.

Best regards.