如何使用 MS Open XML SDK 检索一些图像数据和格式?

发布于 2024-11-30 18:44:25 字数 945 浏览 1 评论 0原文

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

如何检索:

  • 来自 DocumentFormat.OpenXml.Presentation.Picture 对象的图像数据?
  • 图像名称和/或类型?

例如,如下:

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

    foreach (SlideId slide_id in presentation.SlideIdList) {
        SlidePart slide_part = doc.PresentationPart.GetPartById(slide_id.RelationshipId) as SlidePart;
        if (slide_part == null || slide_part.Slide == null)
            continue;
        Slide slide = slide_part.Slide;
        foreach (var pic in slide.Descendants<Picture>()) {
            // how can one obtain the pic format and image data?
        }
    }
}

我意识到我有点在这里寻求现成的答案,但我只是无法在任何地方找到足够好的文档来自己解决这个问题。

This is a follow-up question to How can I retrieve images from a .pptx file using MS Open XML SDK?

How can I retrieve:

  • The image data from a DocumentFormat.OpenXml.Presentation.Picture object?
  • The image name and/or type?

in, say, the following:

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

    foreach (SlideId slide_id in presentation.SlideIdList) {
        SlidePart slide_part = doc.PresentationPart.GetPartById(slide_id.RelationshipId) as SlidePart;
        if (slide_part == null || slide_part.Slide == null)
            continue;
        Slide slide = slide_part.Slide;
        foreach (var pic in slide.Descendants<Picture>()) {
            // how can one obtain the pic format and image data?
        }
    }
}

I realize that I'm kinda asking for out-of-the-oven answers here, but I just can't find good enough docs anywhere to figure it out on my own.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-12-07 18:44:25

首先,获取对图片的 ImagePart 的引用。 ImagePart 类提供您正在查找的信息。下面是一个代码示例:

string fileName = @"c:\temp\myppt.pptx";
using (var doc = PresentationDocument.Open(fileName, false))
{        
  var presentation = doc.PresentationPart.Presentation;

  foreach (SlideId slide_id in presentation.SlideIdList)
  {          
    SlidePart slide_part = doc.PresentationPart.GetPartById(slide_id.RelationshipId) as SlidePart;
    if (slide_part == null || slide_part.Slide == null)
      continue;
    Slide slide = slide_part.Slide;

    // from a picture
    foreach (var pic in slide.Descendants<Picture>())
    {                                
      // First, get relationship id of image
      string rId = pic.BlipFill.Blip.Embed.Value;

      ImagePart imagePart = (ImagePart)slide.SlidePart.GetPartById(rId);

     // Get the original file name.
      Console.Out.WriteLine(imagePart.Uri.OriginalString);                        
      // Get the content type (e.g. image/jpeg).
      Console.Out.WriteLine("content-type: {0}", imagePart.ContentType);           

      // GetStream() returns the image data
      System.Drawing.Image img = System.Drawing.Image.FromStream(imagePart.GetStream());

      // You could save the image to disk using the System.Drawing.Image class
      img.Save(@"c:\temp\temp.jpg"); 
    }                    
  }
}

出于同样的原因,您还可以使用以下代码迭代 SlidePart 的所有 ImagePart:

// iterate over the image parts of the slide part
foreach (var imgPart in slide_part.ImageParts)
{            
  Console.Out.WriteLine("uri: {0}",imgPart.Uri);
  Console.Out.WriteLine("content type: {0}", imgPart.ContentType);                        
}

希望这会有所帮助。

First, obtain a reference to the ImagePart of your Picture. The ImagePart class provides the information you are looking for. Here is a code sample:

string fileName = @"c:\temp\myppt.pptx";
using (var doc = PresentationDocument.Open(fileName, false))
{        
  var presentation = doc.PresentationPart.Presentation;

  foreach (SlideId slide_id in presentation.SlideIdList)
  {          
    SlidePart slide_part = doc.PresentationPart.GetPartById(slide_id.RelationshipId) as SlidePart;
    if (slide_part == null || slide_part.Slide == null)
      continue;
    Slide slide = slide_part.Slide;

    // from a picture
    foreach (var pic in slide.Descendants<Picture>())
    {                                
      // First, get relationship id of image
      string rId = pic.BlipFill.Blip.Embed.Value;

      ImagePart imagePart = (ImagePart)slide.SlidePart.GetPartById(rId);

     // Get the original file name.
      Console.Out.WriteLine(imagePart.Uri.OriginalString);                        
      // Get the content type (e.g. image/jpeg).
      Console.Out.WriteLine("content-type: {0}", imagePart.ContentType);           

      // GetStream() returns the image data
      System.Drawing.Image img = System.Drawing.Image.FromStream(imagePart.GetStream());

      // You could save the image to disk using the System.Drawing.Image class
      img.Save(@"c:\temp\temp.jpg"); 
    }                    
  }
}

By the same token you could also iterate over all ImagePart's of a SlidePart using the following code:

// iterate over the image parts of the slide part
foreach (var imgPart in slide_part.ImageParts)
{            
  Console.Out.WriteLine("uri: {0}",imgPart.Uri);
  Console.Out.WriteLine("content type: {0}", imgPart.ContentType);                        
}

Hope, this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文