Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open xml getting images from .pptx file

Tags:

c#

openxml

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

like image 438
petko_stankoski Avatar asked Nov 27 '25 13:11

petko_stankoski


1 Answers

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());
like image 76
petko_stankoski Avatar answered Nov 30 '25 03:11

petko_stankoski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!