简体   繁体   中英

c# extraction of images from .pptx file

I have the following code for image extraction from .pptx file:

  public static void GetImages()
    {
        var doc = PresentationDocument.Open(@"D:\Peak Sourcing\Work\ppt_test\xx.pptx", true);

        var presentationPart = doc.PresentationPart;

        var slidePart = presentationPart.GetPartsOfType<SlidePart>().First();

        var imagePart = slidePart.GetPartsOfType<ImagePart>().First();

        var stream = imagePart.GetStream();

        var img = Image.FromStream(stream);

        img.Save(@"D:\Peak Sourcing\Work\ppt_test\test-output.png");

    }

The code works, but is extracts just some random image from .ppt file. How to extract all images in order?

Looks like it is not a random image but the first one. Try to iterate over presentationPart.GetPartsOfType<SlidePart>() and slidePart.GetPartsOfType<ImagePart>() using foreach loop.

edit:

here is working code for all images. it generates random file name:

 public static void GetImages()
        {
            var doc = PresentationDocument.Open(@"E:\Dev\Stuff\ConsoleApplication1\ConsoleApplication1\bin\Debug\test.pptx", true);
        var presentationPart = doc.PresentationPart;

        foreach (var slide in presentationPart.GetPartsOfType<SlidePart>())
        {
            foreach (var image in slide.GetPartsOfType<ImagePart>())
            {
                if (image != null)
                {
                    var stream = image.GetStream();

                    var img = Image.FromStream(stream);
                    img.Save(@"E:\Dev\Stuff\ConsoleApplication1\ConsoleApplication1\bin\Debug\" + System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(), "jpg"));
                }
            }
        }
    }

I did some manual iteration, but again, I do not get intended image. In this example, it should return the first image from 6th slide, but it returns some other image from a different slide:

public static void GetImages()
        {
            var doc = PresentationDocument.Open(@"D:\Peak Sourcing\Work\ppt_test\xx.pptx", true);

            var presentationPart = doc.PresentationPart;

            int number_of_slides = presentationPart.GetPartsOfType<SlidePart>().Count(); //get number of slides in .ppt




            var slidePart = presentationPart.GetPartsOfType<SlidePart>().ElementAt(5); //look for image inside this slide number + 1

            int number_of_images = slidePart.GetPartsOfType<ImagePart>().Count(); //get number of images in current slide

            var imagePart = slidePart.GetPartsOfType<ImagePart>().ElementAt(0); //should return first image of selected slide, but it doesn't....it returns an image from some other slide

            var stream = imagePart.GetStream();

            var img = Image.FromStream(stream);

            img.Save(@"D:\Peak Sourcing\Work\ppt_test\test-output.jpg");

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM