Replacing images

Hi there,
I have a document with images and I need to dynamically replace the images. I have found some code to find all of the images, however, how do I replace it with another image?
Here is the code to find all the images…

// Get all floating shapes in the document
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
// Iterate through all floating shapes in the document
foreach (Shape shape in shapes)
{
    // replace image
}

Thanks, John

The following simple code replaces all images in the document with an image from Aspose.Words.gif file:

// Get all floating shapes in the document
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
// Iterate through all floating shapes in the document
foreach (Shape shape in shapes)
{

    // Check if the shape is an image
    if (shape.ShapeType == ShapeType.PictureFrame)
    {
        shape.ImageData.SetImage("C:\Images\Aspose.Words.gif");
    }
}

Best regards,

Thanks for the code, works great.
Question tho…
The original image in the document is a bit bigger than the one I am inserting, so it gets stretched a bit. Is it possible to lock the height and width to strictly 100%?
Currently, the inserted image is 137% height and 100% width…and I want it to be 100% x 100%
John

Good question. It is possible, but not directly in the current API, but we will consider adding some helper methods for this.
Here is the story:
In Aspose.Words document model an image or any shape has Width and Height specified in points (a point is 1/72inch). There is no image scale in Aspose.Words at this time.
When you have an image, say in a file, it has width and height in pixels. But pixels are resolution dependent and therefore the image typically has resolution DPI (dots per inch).
Say you have an existing shape in the document, for example100x100 points size. When you replace the image inside this shape, the new image size is probably different from the original. Note that the size of the shape that contains the image is not changed when you just change the image. The shape remains 100x100 points. Your image is scaled (could be even distorted) to fill the 100x100 points shape. Microsoft Word displays you a calculated scale factor for the shape when go into shape properties.
The key is: image has its size independent of the size of the shape that contains the image. When they both match, Microsoft Word reports 100% scale, otherwise the scale is different.
Another key is: image size in pixels and shape size is in points.
In your scenario, to keep the 100% scale for the image you simply need to update the shape size when you insert the image. But you need to remember to convert image size from pixels to points. Also need to remember to do that at the proper resolution.
Here is what you need to do:

Image image = Image.FromFile(myFileName);
shape.ImageData.SetImage(image);
shape.Width = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
shape.Height = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

romank:
Good question. It is possible, but not directly in the current API, but we will consider adding some helper methods for this.

I am just wondering if this has been implemented in the latest version?
Thanks,
Vlado

Hi

Thanks for your request. If you specify negative width and height during inserting an image, then the image will be scaled to 100%. Please see the following link for more information:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/insertimage/
Please let me know in case of any issues.
Best regards.

Alex,
thanks for your response. My question was about replacing an existing image in Word and your link points so Inserting of one. Or do I see it incorrectly? What I want to achieve? I want to have a Word document as a template with dummy images in it and use Aspose.Chart to create images of charts which I would insert instead of the dummy images in Word. What is the best way to do it?
Thanks.
Vlado

Hi Vlado,

Thanks for your inquiry. You can use the code Vladimir provided above to replace an existing images with new images. But I think it is better to use Mail Merge to achieve this. You can insert special mergefields into the document (with name like “Image:myimg”). In this case, in the data source, you should have field with name “myimg” and value of this field should be image data of path to the image. Here you can find code example:
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
Hope this helps.
Best regards.