1分鐘學會在 C# 中將 PPTX/PPT 轉換爲 PNG 圖像

在某些情況下,可能需要將 PowerPoint 演示文稿中的幻燈片轉換爲圖像。例如,在您的 Web 或桌面應用程序中嵌入演示文稿、生成縮略圖等。PNG是使用無損壓縮的最流行的圖像格式之一。因此,在本文中,您將學習如何使用 C# 將 PowerPoint PPTX 或 PPT 中的幻燈片轉換爲 PNG 圖像。

  • 將 PowerPoint PPTX 或 PPT 轉換爲 PNG

爲了將 PPTX 或 PPT 演示文稿轉換爲 PNG,我們將使用Aspose.Slides for .NET,它是一個功能豐富的 API,可讓您使用 C# 創建、修改和轉換 PowerPoint 和 OpenOffice 演示文稿。

>>你可以下載Aspose.Slides 最新版測試體驗。

在 C# 中將 PowerPoint PPTX 或 PPT 轉換爲 PNG

以下是使用 C# 將 PowerPoint PPTX 中的幻燈片轉換爲 PNG 圖像的步驟。

  • 首先,創建Presentation 類的一個實例 來加載演示文稿。
  • 循環每一個 I幻燈片式 的 Presentation.Slides 集合。
  • 定義生成的 PNG 圖像的尺寸。
  • 使用ISlide.GetThumbnail(float ScaleX, float ScaleY) 方法生成每張幻燈片的圖像,並將圖像 的引用放入 Bitmap 對象。
  • 最後,使用Bitmap.Save(String, System.Drawing.Imaging.ImageFormat.Png) 方法將圖像保存爲 PNG 。

以下代碼示例展示瞭如何將 PowerPoint PPTX 轉換爲 PNG。

// Load PowerPoint presentation
using (Presentation pres = new Presentation("presentation.pptx"))
{
    // User defined dimension
    int desiredX = 1200;
    int desiredY = 800;

    // Getting scaled value of X and Y
    float ScaleX = (float)(1.0 / pres.SlideSize.Size.Width) * desiredX;
    float ScaleY = (float)(1.0 / pres.SlideSize.Size.Height) * desiredY;

    foreach (ISlide sld in pres.Slides)
    {
        // Create a full scale image
        Bitmap bmp = sld.GetThumbnail(ScaleX, ScaleY);

        // Save the image to disk in PNG format
        bmp.Save(String.Format("slide_{0}.png", sld.SlideNumber), System.Drawing.Imaging.ImageFormat.Png);
    }
}

如果您有任何疑問或需求,請隨時加入Aspose技術交流羣(761297826),我們很高興爲您提供查詢和諮詢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章