2011-08-21 9 views
6

यह How can I retrieve images from a .pptx file using MS Open XML SDK?एमएस ओपन एक्सएमएल एसडीके का उपयोग करके मैं कुछ छवि डेटा और प्रारूप कैसे प्राप्त कर सकता हूं?

लिए एक अनुवर्ती सवाल यह है कि मैं कैसे प्राप्त कर सकते हैं: एक 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? 
     } 
    } 
} 

मुझे लगता है कि मैं थोड़े यहाँ बाहर के-ओवन जवाब के लिए पूछ रहा हूँ, लेकिन मैं सिर्फ अच्छा पर्याप्त डॉक्स कहीं भी नहीं मिल सकता है इसे अपने आप समझने के लिए।

उत्तर

10

पहले, अपने चित्र का 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);       
} 

आशा, इस मदद करता है।

संबंधित मुद्दे