2011-08-21 17 views
6

Bu How can I retrieve images from a .pptx file using MS Open XML SDK?MS Open XML SDK kullanarak bazı görüntü verilerini ve biçimlerini nasıl alabilirim?

için bir takip sorudur nasıl alabilirsiniz:

  • görüntü verilerini bir DocumentFormat.OpenXml.Presentation.Picture nesneden?
  • Resim adı ve/veya türü?
  • diyelim ki, içinde

aşağıdaki:

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? 
     } 
    } 
} 

ben biraz burada-of-the-fırın cevapları soruyorum fark, ama sadece her yerde yeterince iyi dokümanlar bulamıyor kendi başıma bulmak için.

cevap

10

İlk önce, Resminizin ImagePart'ına bir başvuru alın. ImagePart sınıfı, aradığınız bilgiyi sağlar.

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

da aşağıdaki kodu kullanarak bir SlidePart tüm ImagePart 's tekrarlayabilirsiniz Aynı şekilde: İşte bir kod örneği olan

// 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, bu yardımcı olur.

İlgili konular