Reputation: 10713
I have a Windows forms app in .Net 4.0. I work in C#. I want to grab an image from a given slide in a .pptx file.
This code gets every image on the slide:
public static SlidePart GetSlidePart(PresentationDocument presentationDocument, int slideIndex)
{
if (presentationDocument == null)
{
throw new ArgumentNullException("presentationDocument", "GetSlidePart Method: parameter presentationDocument is null");
}
int slidesCount = CountSlides(presentationDocument);
if (slideIndex < 0 || slideIndex >= slidesCount)
{
throw new ArgumentOutOfRangeException("slideIndex", "GetSlidePart Method: parameter slideIndex is out of range");
}
PresentationPart presentationPart = presentationDocument.PresentationPart;
if (presentationPart != null && presentationPart.Presentation != null)
{
Presentation presentation = presentationPart.Presentation;
if (presentation.SlideIdList != null)
{
var slideIds = presentation.SlideIdList.ChildElements;
if (slideIndex < slideIds.Count)
{
string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
return slidePart;
}
}
}
return null;// No slide found
}
But, how to convert slidePart to an image which will be shown in my Windows form (in imageList or something similar)?
Upvotes: 3
Views: 2774
Reputation: 10713
Found a way:
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
Slide slide = slidePart.Slide;
ImagePart imagePart = (ImagePart)slide.SlidePart.GetPartById("rId3");
System.Drawing.Image img = System.Drawing.Image.FromStream(imagePart.GetStream());
Upvotes: 3