Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图片插入到现有形状_C#_Aspose_Aspose Slides - Fatal编程技术网

C# 将图片插入到现有形状

C# 将图片插入到现有形状,c#,aspose,aspose-slides,C#,Aspose,Aspose Slides,在PPTX文档中插入图像时遇到问题 我想基于幻灯片蒙版创建一张新幻灯片。我使用此代码将图片设置为现有形状 class Program { public static void Run() { string dataDir = ConfigurationManager.AppSettings["directoryToSave"]; string srcDir = String.Concat(Configura

在PPTX文档中插入图像时遇到问题

我想基于幻灯片蒙版创建一张新幻灯片。我使用此代码将图片设置为现有形状

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

    }

我在生成的文件中得到的是一个带有背景图片和占位符的形状。您可以在该链接中找到所有资源

我已经观察到您共享的需求,并希望共享占位符不是实际形状,而是形状的骨架。为了方便您,我创建了一个示例代码,它可以帮助您实现您的需求

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);
}
我在Aspose担任支持开发者/布道者