Free Support Forum - aspose.com

Replacing a Picture on a Slide

Hi, so I’m trying to replace a picture on a slide with some other picture and I can’t seem to get it working.


Here is some condensed code to show what I’m trying:

foreach (ShapeEx shp in slds[slds.Count - 1].Shapes)
{
else if (shp.Name.Contains(“Picture”))
{
//Set the fill type to Picture
shp.FillFormat.FillType = FillTypeEx.Picture;

//Set the picture fill mode
shp.FillFormat.PictureFillFormat.PictureFillMode = PictureFillModeEx.Stretch;

//Set the picture
System.Drawing.Image img = (System.Drawing.Image)new Bitmap(localizedImage);
ImageEx imgx = srcPres.Images.AddImage(img);
}
}

I was following the example posted in the documents section titled Fill ShapesEX with Picture (http://www.aspose.com/docs/display/slidesnet/Filling+ShapesEx+with+Picture)
but the picture doesn’t seem to change.

Using some debugging I found out that This shape in particular is a PictureEx, so I’m not sure how its even entering the foreach loop since every conversion i have tried seems to say you cant cast ShapeEx to PictureEx. Any tips are appreciated.

Thanks in advanced,
-Drew

Hi Drew,


Thanks for your interest in Aspose.Slides.

I have observed the sample code shared and like to share that you are adding the image to presentation image collection and have missed to assign the added image to shape. Please visit this documentation link for your kind reference.

shp.FillFormat.PictureFillFormat.Picture.Image = imgx;

Please share if I may help you further in this regard.

Many Thanks,

Hi Mudassir,


Thank you for the response. However, I had accidentally forgot to include that code in my original post, and yet when I run the code I still only see the original picture place holder on all newly created slides. Are there any further tips you can provide?

This is the un-trimmed code I am currently using, that lies in the foreach statement

else if (shp.Name.Contains(“Picture”))
{
// Specify where to store downloaded image and under what name to // store it
string localizedImage = string.Format(_directory + “coverImage.jpg”);
try
{
//Download the image client.DownloadFile(link, localizedImage);
//Set the fill type to Picture
shp.FillFormat.FillType = FillTypeEx.Picture;

//Set the picture fill mode
shp.FillFormat.PictureFillFormat.PictureFillMode = PictureFillModeEx.Stretch;

//Set the picture
System.Drawing.Image img = (System.Drawing.Image)new Bitmap(localizedImage);
ImageEx imgx = srcPres.Images.AddImage(img);

var centerX = shp.Frame.CenterX;// shp.X + (shp.Width / 2);
var centerY = shp.Frame.CenterY;//shp.Y + (shp.Height / 2);
shp.Height = shp.Width * ((float)imgx.Height / imgx.Width);
shp.X = centerX - (shp.Width / 2);
shp.Y = centerY - (shp.Height / 2);

shp.FillFormat.PictureFillFormat.Picture.Image = imgx;
img.Dispose();
}
catch
{
Console.WriteLine(“Error: Image search failed”);
}
}

Thank you,
Drew

Hi Drew,


I have worked over the requirements shared by you and have shared the sample project for your convenience. I have been able to change the picture on my end. Please use the sample project and shared if there is still an issue on your end.

Many Thanks,