简体   繁体   中英

Accessing Images in PowerPoint file via VSTO Add-In (C#)

I am trying to build a PowerPoint add-in that will allow me to do a one-click optimization of the current presentation by removing unused master slides and converting huge 24-bit PNG files to slightly-compressed JPGs.

I have the first part handled already, and now I'm working on the images. Although I can easily find the Shape object containing the image, I cannot find a way to access the source image through the managed API. At best, I can copy the shape to the clipboard, which does give me the image but in a different format (MemoryBmp).

using PPTX = Microsoft.Office.Interop.PowerPoint;

...

foreach (PPTX.Slide slide in Globals.ThisAddIn.Application.ActivePresentation.Slides)
{
    foreach (PPTX.Shape shape in slide.Shapes)
    {
        if (shape.Type == MsoShapeType.msoPicture)
        {
            // Now, how can I access the source image contained within this shape?

            // I -can- copy it via the clipboard, like this:
            shape.Copy();
            System.Drawing.Image image = Clipboard.GetImage();

            // ...but image's format reflects a MemoryBmp ImageFormat, as noted here: 
            // https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/ImageFormat.cs
            // ...which doesn't help me determine if the source image is something that should be optimized.
        }
    }
}

Obviously, I can get to the images directly via other methods (eg accessing the contents of the file as a ZIP, or using OpenXML SDK), but I'm trying to perform all the steps from within the already-opened presentation (which means I can't update the open file). Any thoughts on how I can do this?

You could use a Microsoft.Office.Interop.PowerPoint.Shape Export function.

void Export(string PathName, PpShapeFormat Filter, int ScaleWidth = 0, int ScaleHeight = 0, PpExportMode ExportMode = PpExportMode.ppRelativeToSlide)

There is a very little documentation of this function. I've found it in the Shape interface definition. So your code would look something like:

shape.Export(<some_file>, PpShapeFormat.ppShapeFormatPNG);

More info on MSDN

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