使用 C# 在 Powerpoint 中获取图像

问题描述 投票:0回答:1

我想知道是否有一种方法可以使用 c# 获取 powerpoint 幻灯片中的图像。 我尝试使用 openXML 执行此操作,下面的代码检索幻灯片中的文本。 但我找不到获取图像的方法

using (var stream = file.OpenReadStream())
{
   using (var doc = PresentationDocument.Open(stream, false))
   {
      var part = doc.PresentationPart;
      var slideIds = part.Presentation.SlideIdList.ChildElements;
      var slideCount = doc.PresentationPart.SlideParts.Count();

      for(var i = 0; i < slideCount; i++)
      {
         var relId = (slideIds[i] as SlideId).RelationshipId;

         var slide = (SlidePart)part.GetPartById(relId);
         var slideTexts = slide.Slid.Descendants<DocumentFormat.OpenXml.Drawing.Text>();
         var slideImages = slide.Slid.Descendants<DocumentFormat.OpenXml.Drawing.Picture>(); //Doesn't work
      }
   }
}
c# powerpoint
1个回答
0
投票

您想将图像从 C# 转换为 ppt。我在 ChatGPT 中找到了这一点。你可以试试。

使用系统; 使用 PowerPoint = Microsoft.Office.Interop.PowerPoint;

命名空间 ImageToPowerPoint { 班级计划 { 静态无效主(字符串[]参数) { // 创建一个 PowerPoint 应用程序对象。 PowerPoint.Application pptApp = new PowerPoint.Application();

        // Create a new presentation.
        PowerPoint.Presentation presentation = pptApp.Presentations.Add();

        // Add a new slide.
        PowerPoint.Slide slide = presentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutText);

        // Insert an image into the slide.
        var imagePath = "path_to_your_image.jpg"; // Provide the actual path to your image file.
        PowerPoint.Shape picture = slide.Shapes.AddPicture(imagePath, Left: 50, Top: 50, Width: 300, Height: 200);

        // Save the presentation.
        var outputPath = "output_presentation.pptx"; // Provide the desired output path.
        presentation.SaveAs(outputPath);

        // Close and release resources.
        presentation.Close();
        pptApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(pptApp);

        Console.WriteLine("Image converted to PowerPoint successfully.");
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.