PictureFrame Replace image replacing all the images in presentation

Below code replacing all the images in presentation, I want only picture with particular shape to get replace

  PictureFrame pf = shapes[j] as PictureFrame;
                            if (pf != null)
                            {
                                if (pf.AlternativeText.ToLowerInvariant().IndexOf(("photo_" + i)) > -1)
                                {
      
               
                                   /strImagePath = "\\files\\Employee_Images\\42PJN.jpg";

                                    byte[] data = File.ReadAllBytes(strImagePath);
      
                                    pf.PictureFormat.Picture.Image.ReplaceImage(data);

 


                                }

                            }

@vish02chouhan,

I suggest you to please visit this documentation link for your convenience. Please share, if you encounter any issue on your end.

Hi,
I have already go through this document. There are many shapes on my slides which contains pictures and I want to replace individual picture frame image.

Can you please help me on replacing image in that particular picture frame.

Please refer my above code

@vish02chouhan,

In that case you need to first proceed to desired Picture Frame. You may know the index of shape or identify that on the basis of Alternative Text Property. Then you can simply add a new image in presentation images collection and set that to your desired Picture frame by simply following similar mechanism of adding Picture frame in slides. The only difference here would be that instead of adding a new Picture Frame, you will access existing one and set new image for that.

picframe.PictureFormat.Picture.Image=new_image;//new_image is your newly added image in collection

I had the same issue with my project now. The problem was, I added an image to the presentation and then copy and paste the same image multiple times. I then gave each image a unique name.
I then tried to replace each image in code by name, but it kept on replacing all the images with the last image i process.

SOLUTION !!
Make sure not to duplicate the same image in your presentation. Add/Upload a unique image for every image placeholder on your presentation slide.

@Newsclip,

I have observed your following comments.

Aspose.Slides keeps single copy of image if same image file is added to image collection. Its good, if you have figured out the solution on your end. Please feel free to contact us for any issues on your end.

@mudassir.fayyaz,

We are facing the same issue, but we can’t use distinct image for each placeholder because we can’t explain the same to user. Could you please give some other solution on this.

@kasima1,

I have tried understanding your requirements and have not been able to completely understand that. Can you please provide the details of requirements on your end so that I may proceed further to help you out.

We are facing one issue in PPT.

Issue:
I have a PPT document with 5 images, with different Alt text.

When I replace the first image with new image it replacing all the five images.

Please give the solution for this issue.

Code:

var licFile = new Aspose.Slides.License();
licFile.SetLicense(“Aspose.Slides.lic”);
var pres = new Presentation(documentPath);
var shapes = pres.Slides.Where(x => x.Shapes != null && x.Shapes.Any(y => imageNameandPath.ContainsKey(y.AlternativeText))).Select(x => x.Shapes);
foreach (var slides in shapes)
{
foreach (var shape in slides)
{
var img = shape as PictureFrame;
//https://docs.aspose.com/display/slidesnet/Replacing+Images+inside+Presentation+Image+Collection
img.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
var oldImage = img.PictureFormat.Picture.Image;
//once cropping done. need to resize the image
var aspectCropFilePath = ResizeImageSize(imageNameandPath[altText].CropPath, img.Width, img.Height);
oldImage.ReplaceImage(File.ReadAllBytes(aspectCropFilePath));
}
}

This below link also not working because it has same image collection:
https://forum.aspose.com/t/how-do-i-replace-a-specific-image-in-ppt-with-the-passed-image/7415

Please find the attached Sample PPT.Presentation1.zip (22.9 KB)

@kasima1,

I have observed the sample code as well as the presentation file shared by you. Actually, there is only one image added in Presentation image collection (pres.Images) and that is used in all 5 shapes. So, when you replace the image, it is going to get replaced for all shapes where it has been referred. Its not an issue or limitation of API. You may need to add separate images for all shapes and that you can then replace. I hope the shared elaboration will be helpful.

Hi @mudassir.fayyaz,

I understand your comments, but the same scenario is working in word. We have used the following code for Word. So Could you please give some other solution for PPT, Like replace image by its Alternative text, SETImage function etc.

Code used for Word:

var licFile = new Aspose.Words.License();
licFile.SetLicense(“Aspose.Words.lic”);
var doc = new Document(documentPath);
foreach (Aspose.Words.Drawing.Shape img in doc.GetChildNodes(NodeType.Shape, true))
{
if (string.IsNullOrEmpty(img.AlternativeText) ||
(img.ShapeType != Aspose.Words.Drawing.ShapeType.Image &&
img.ShapeType != Aspose.Words.Drawing.ShapeType.Rectangle)) continue;
if (imageNameandPath.ContainsKey(img.AlternativeText))
{
var aspectCropFilePath = “”; // New image path comes here;
img.ImageData.SetImage(aspectCropFilePath);
}
}

@kasima1,

I have observed your comments. Can you please visit this thread link. This will help you to achieve requirements. Please share feedback with us if there is still an issue.

This is an old thread, but for the next guy/gal with this issue. The trick is to set the .Image property and to NOT use the .Image.ReplaceImage() method. The ReplaceImage method is good for updating the image content for the existing item in the Images collection that is potentially referenced by multiple shapes. If you update the image content then all of the shapes referencing will update too. While nice that you can do this, that is not the goal here.

The goal here is that someone gave you a template where (for convenience) they copy/pasted the same shape multiple times. Your code is now going to place a distinct image into each one of these frames based on your business logic. Again if you use ReplaceImage you’ll be updating the referenced image. Instead you create a new image in the images collection and you SET the image property.

Presentation presentation = new Presentation(powerPointPath);
var pictureFrame1 = presentation.Slides[0].Shapes[0] as PictureFrame;
var pictureFrame2 = presentation.Slides[0].Shapes[1] as PictureFrame;

pictureFrame1.PictureFormat.Picture.Image = presentation.Images.AddImage(stream1);
pictureFrame2.PictureFormat.Picture.Image = presentation.Images.AddImage(stream2);

You are right. When you will using ReplaceImage() method, it will change the image in in image collection of presentation. Therefore, where ever this image is referred in slides, it will be replaced with new one. This is handy in many situations. One such scenario is that there is some company logo or watermark image used in all slides. You want to change that and in this case Replace image will be helpful.

When you will use this call, a new image will be added in Image collection of presentation. This is useful in scenario where you ought to set the image of particular shape only.

1 Like