How to get some data and image format using MS Open XML SDK?

This is the next question for How to get images from a .pptx file using the MS Open XML SDK?

How can I get:

  • Image data from a DocumentFormat.OpenXml.Presentation.Picture object?
  • 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 understand that I'm asking for answers to over-the-counter questions here, but I just can't find enough documents anywhere to figure it out on my own.

+6
source share
1 answer

First get a link to the ImagePart of your image. The ImagePart class provides the information you are looking for. Here is a sample code:

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

In addition, you can also iterate over all ImagePart from 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.

+10
source

Source: https://habr.com/ru/post/895519/


All Articles