Insert picture to an existing shape (C# . NET)

Hello;

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

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

 public void setShapePicture(Presentation doc, ILayoutSlide layoutSlide, string shapeName, System.Drawing.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;
        }

The problem when I generate a pptx file I get a shape with a placeholder and a picture as background of the shape.

Capture.PNG (12.2 KB)

So How can I set a picture to an existing shape?

@radouani1984,

I have observed your requirements and request you to please share the source presentation file along with source image, generated presentation and desired output presentation. I will use the shared information and try to give you desired output.

Hello;
Thanks for you reply.
I share with you all the elements as you asked.

 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();
            }
        }
     
    }

Files
https://gofile.io/?c=gvGEz7

@radouani1984,

I have observed your requirements and suggest you to please visit the following thread link for your kind reference to serve the purpose. I hope this will be helpful.

1 Like

Hello;
It works Thank you very much.
Could you please have a look at his post? Adding text to multiple paragraphs failing (C# .NET)
Thanks :slight_smile:

@radouani1984,

It’s good to hear that suggested code worked on your end. I have responded to the referred thread as well. Please check the concerned thread for response.