通过VSTO插件访问PowerPoint文件中的图像(C#)。

问题描述 投票:0回答:1

我正在尝试建立一个PowerPoint插件,它将允许我通过删除未使用的主幻灯片,并将巨大的24位PNG文件转换为稍微压缩的JPG,对当前的演示文稿进行一键优化。

我已经处理了第一部分,现在我正在处理图像。虽然我可以很容易地找到包含图像的Shape对象,但我无法找到通过托管API访问源图像的方法。我最多只能把形状复制到剪贴板上,而剪贴板上的图像 是否 给我一个不同格式的图像(MemoryBmp).显然,我可以通过其他方法(例如以ZIP格式访问文件内容,或使用OpenXML SDK)直接获取图像,但我试图从内部执行所有步骤。

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

显然,我可以通过其他方法直接获取图像(例如,以ZIP的形式访问文件内容,或使用OpenXML SDK),但我试图在已经打开的演示文稿中执行所有步骤(这意味着我不能更新打开的文件)。有什么想法可以让我做到这一点?

c# vsto powerpoint
1个回答
0
投票

你可以使用一个 Microsoft.Office.Interop.PowerPoint.Shape Export 函数。

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

关于这个函数的文档非常少,我在 Shape 接口定义,所以你的代码会像这样。

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

更多信息请访问MSDN

© www.soinside.com 2019 - 2024. All rights reserved.