2011-08-21 16 views
6

Jest to kontynuacja pytanie do How can I retrieve images from a .pptx file using MS Open XML SDK?Jak mogę pobrać niektóre dane obrazu i formatować za pomocą MS Open XML SDK?

Jak mogę odzyskać:

  • dane obrazu z obiektu DocumentFormat.OpenXml.Presentation.Picture?
  • Nazwa i/lub typ obrazu?

w, powiedzmy, co następuje:

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

Zdaję sobie sprawę, że jestem trochę prosząc o out-of-the-piekarnik odpowiedzi tutaj, ale po prostu nie mogę znaleźć wystarczająco dużo dobrych dokumenty gdziekolwiek Sam to rozgryźć.

Odpowiedz

10

Najpierw należy uzyskać odniesienie do ImagePart w obrazie. Klasa ImagePart zapewnia informacje, których szukasz. Oto przykładowy kod:

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

Na tej samej zasadzie można również iteracyjne nad wszystkie ImagePart dnia na SlidePart za pomocą następującego kodu:

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

nadziei, że to pomoże.

Powiązane problemy