Insert Picture to an existing shape

762 views Asked by At

I encounter a problem when inserting an image in PPTX document.

I want to create a new slide based on slide mask. I used this code to set picture in existing a shape

class Program
    {

        public static void Run()
        {
            string dataDir = ConfigurationManager.AppSettings["directoryToSave"];
            string srcDir = String.Concat(ConfigurationManager.AppSettings["Source"], "Master.pptx");
            string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            //string file = Path.Combine(appData, srcDir);
            using (Presentation presentation = new Presentation(srcDir))
            {
                IMasterLayoutSlideCollection layoutSlides = presentation.Masters[0].LayoutSlides;
                ILayoutSlide layoutSlide = null;

                foreach (ILayoutSlide titleAndObjectLayoutSlide in layoutSlides)
                {
                    if (titleAndObjectLayoutSlide.Name == "TITRE_CONTENU")
                    {
                        layoutSlide = titleAndObjectLayoutSlide;
                        break;
                    }
                }
                string filePath = String.Concat(ConfigurationManager.AppSettings["Source"], @"Logos\bp.png");
                Image imgs = (Image)new Bitmap(filePath);

                setShapePicture(presentation, layoutSlide, "picture", imgs);
                presentation.Slides.InsertEmptySlide(0, layoutSlide);
                presentation.Save(dataDir + "AddLayoutSlides_out.pptx", SaveFormat.Pptx);
            }
        }
        public static void setShapePicture(Presentation doc, ILayoutSlide layoutSlide, string shapeName, Image image)
        {
            var logo_shape = layoutSlide.Shapes.SingleOrDefault(r => r.Name.Equals(shapeName));
            IPPImage imgx = doc.Images.AddImage(image);
            //Add Picture Frame with height and width equivalent of Picture
            logo_shape.FillFormat.FillType = FillType.Picture;
            // Set the picture fill mode
            logo_shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
            // Set the picture
            logo_shape.FillFormat.PictureFillFormat.Picture.Image = imgx;
        }
        static void Main(string[] args)
        {
            try
            {
                var path = ConfigurationManager.AppSettings["sourceAsposeLicensePath"];
                License license = new License();
                license.SetLicense(path);
                Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error" + ex.Message);
            }
            finally
            {
                Console.WriteLine("Terminated");
                Console.ReadKey();
            }
        }

    }

What I get in the generated file is a shape with background picture and a placeholder. You can find all resources in this link MasterSlide+Logos+Output

1

There are 1 answers

0
Mudassir On

I have observed the requirements shared by you and like to share that placeholder is not an actual shape but gives a skeleton for shape. I have created a sample code for your convenience that may help you in achieving your requirements.

public static void TestPlaceholderImage()
{
    String path = "C:\\Aspose Data\\";
    Presentation pres = new Presentation(path + "TestPicture.pptx");

    foreach (ISlide slide in pres.Slides)
    {
        foreach (IShape shape in slide.Shapes)
        {
            if (shape.Placeholder != null)
            {
                if (shape.Placeholder.Type == PlaceholderType.Picture)
                {
                    // Instantiate the ImageEx class
                    System.Drawing.Image img = (System.Drawing.Image)new Bitmap(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
                    IPPImage imgx = pres.Images.AddImage(img);

                    shape.FillFormat.FillType = FillType.Picture;
                    shape.FillFormat.PictureFillFormat.Picture.Image = imgx;

                    if (shape is AutoShape)
                    {
                        ITextFrame text = ((IAutoShape)shape).TextFrame;
                        text.Text = " ";
                        text.Paragraphs[0].ParagraphFormat.Bullet.Type = BulletType.None;

                    }
                }
            }
        }
    }

    pres.Save(path + "Addedpic.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

I am working as Support developer/ Evangelist at Aspose.

SampleData.zip