kjoetools
kjoetools

Reputation: 548

Replace image in a shape, not add a new one

I have a VSTO written in VB.NET and want to maximize a selected image to the background. Documents tell me to remove the old shape and add a new one with AddPicture, but I have a template that includes image shapes already there and AddPicture puts the image in one of those shapes already there.

To prevent images from landing in the wrong shape I really just want to replace the original image with the resized one.

This is what I have:

dim src as PowerPoint.Shape = PowerPoint.Selection.ShapeRange.Item(1)
src.Export(file1, PowerPoint.PpShapeFormat.ppShapeFormatPNG)
dim img as Image = BitMap.FromFile(file1)

Then some image magic, and then:

img.Save(file2, PowerPoint.PpShapeFormat.ppShapeFormatPNG)

Dim _S As PowerPoint.Slide = ppApp.ActiveWindow.View.Slide
Dim _P As PowerPoint.Shape = _S.Shapes.AddPicture2(file2, MsoTriState.msoFalse, MsoTriState.msoTrue, 0, 0, _W, _H)

But that puts the image in the wrong template-shape.

Is there a way to actually replace the image in a shape without adding a new shape?

Upvotes: 1

Views: 851

Answers (1)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

You can't insert an image inside a shape, only (in some cases) inside a placeholder, and you've mentioned that you specifically don't want to do that.

You can use an image to fill a shape, but unless you don't mind the image getting distorted, the aspect ratios of the image and the shape must match.

Suggestion: before adding the new image/shape, iterate through all the shapes on the slide looking for placeholder shapes. If any placeholder shape is of a type that can contain images (Content, Picture and Clip Art placeholders), fill it with dummy content (a few characters of text for Content, any random picture for the other two). Add your image; since the "image-capable" placeholders are already filled, the image will be added as a new shape. Then delete each of the shapes you've added dummy content to. When you delete a placeholder that has content, the content gets deleted and leaves the original empty placeholder behind.

Upvotes: 2

Related Questions