C# 如何使用MS Open XML SDK从.pptx文件检索图像?

C# 如何使用MS Open XML SDK从.pptx文件检索图像?,c#,.net,powerpoint,openxml,openxml-sdk,C#,.net,Powerpoint,Openxml,Openxml Sdk,我开始用它做实验 我目前能够做一些事情,比如检索每张幻灯片中的所有文本,并获得演示文稿的大小。例如,我是这样做的: using (var doc = PresentationDocument.Open(pptx_filename, false)) { var presentation = doc.PresentationPart.Presentation; Debug.Print("width: " + (presentation.SlideSize.Cx / 9525.0

我开始用它做实验

我目前能够做一些事情,比如检索每张幻灯片中的所有文本,并获得演示文稿的大小。例如,我是这样做的:

using (var doc = PresentationDocument.Open(pptx_filename, false)) {
     var presentation = doc.PresentationPart.Presentation;

     Debug.Print("width: " + (presentation.SlideSize.Cx / 9525.0).ToString());
     Debug.Print("height: " + (presentation.SlideSize.Cy / 9525.0).ToString());
}

现在我想检索给定幻灯片中的嵌入图像。有人知道怎么做吗,或者可以给我指一些关于这个主题的文档吗?

首先,你需要抓取
幻灯片部分,你想从中获取图像:

public static SlidePart GetSlidePart(PresentationDocument presentationDocument, int slideIndex)
{
    if (presentationDocument == null)
    {
        throw new ArgumentNullException("presentationDocument", "GetSlidePart Method: parameter presentationDocument is null");
    }

    // Get the number of slides in the presentation
    int slidesCount = CountSlides(presentationDocument);

    if (slideIndex < 0 || slideIndex >= slidesCount)
    {
        throw new ArgumentOutOfRangeException("slideIndex", "GetSlidePart Method: parameter slideIndex is out of range");
    }

    PresentationPart presentationPart = presentationDocument.PresentationPart;

    // Verify that the presentation part and presentation exist.
    if (presentationPart != null && presentationPart.Presentation != null)
    {
        Presentation presentation = presentationPart.Presentation;

        if (presentation.SlideIdList != null)
        {
            // Get the collection of slide IDs from the slide ID list.
            var slideIds = presentation.SlideIdList.ChildElements;

            if (slideIndex < slideIds.Count)
            {
               // Get the relationship ID of the slide.
               string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;

                // Get the specified slide part from the relationship ID.
                SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);

                 return slidePart;
             }
         }
     }

     // No slide found
     return null;
}

从Openxml格式获取图像的最简单方法:

使用任何zip存档库从pptx文件的媒体文件夹中提取图像。这将包含文档中的图像。类似地,您可以手动将extension.pptx替换为.zip并提取以从媒体文件夹中获取图像


希望这能有所帮助。

我很好奇-为什么要使用“/9525.0”?EMU到点的标准除数是“/12700”。如何将SlidePart转换为可以在imageList中的实际图像?此代码似乎假设您知道图像的文件名-对吗?如果我只想检索PPTX文件中的第一个图像或PPTX文件中的所有图像,该怎么办?有没有办法将所有幻灯片转换为图像或svg?问题是“如何使用MS Open XML SDK从.PPTX文件检索图像?”您给出了手动解决方案?
Picture imageToRemove = slidePart.Slide.Descendants<Picture>().SingleOrDefault(picture => picture.NonVisualPictureProperties.OuterXml.Contains(imageFileName));