日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#/VB.NET實現PPT或PPTX轉換為圖像功能_C#教程

作者:Gia- ? 更新時間: 2022-09-24 編程語言

由于大多數便攜式設備支持瀏覽圖片而不支持瀏覽PowerPoint 文件,所以相比較而言,圖像對于用戶而言更加友好。除此之外,將PowerPoint文檔轉換為圖像也可以防止對內容做出修改。在本文中,我將展示如何使用?Spire.Presentation for .NET?在C#/VB.NET程序中,將PowerPoint(PPT 和 PPTX)轉換為 PNG 或 SVG。

安裝 Spire.Presentation for .NET

首先,我們需要將?Spire.Presentation for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。可以從此鏈接下載 DLL 文件,也可以通過NuGet?安裝 DLL 文件。

PM> Install-Package Spire.Presentation

將PPT或PPTX轉換為PNG

C#

using Spire.Presentation;
using System;
using System.Drawing;
using System.Drawing.Imaging;
 
namespace ConvertPowerPointToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Presentation實例
            Presentation presentation = new Presentation();

            //加載一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx");

            //遍歷PowerPoint文檔中的幻燈片并保存為PNG圖片
            for (int i = 0; i < presentation.Slides.Count; i++)
            {
                Image image = presentation.Slides[i].SaveAsImage();
                String fileName = String.Format("圖片{0}.png", i);
                image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }
}

VB.NET

Imports Spire.Presentation
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
 
Namespace ConvertPowerPointToPng
    Class Program
        Shared  Sub Main(ByVal args() As String)
            '初始化Presentation實例
            Dim presentation As Presentation =  New Presentation() 
 
            '加載一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx")
 
            '遍歷PowerPoint文檔中的幻燈片并保存為PNG圖片
            Dim i As Integer
            For  i = 0 To  presentation.Slides.Count- 1  Step  i + 1
                Dim image As Image =  presentation.Slides(i).SaveAsImage() 
                Dim fileName As String =  String.Format("圖片{0}.png",i) 
                image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png)
            Next
        End Sub
    End Class
End Namespace

效果圖

將PPT或PPTX轉換為SVG

C#

using System.Collections.Generic;
using System.IO;
namespace PPTtoSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Presentation實例
            Presentation presentation = new Presentation();

            //加載一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx");

            //將PowerPoint轉換為SVG圖像并以字節形式存儲在列隊中
            Queue<byte[]> svgBytes = presentation.SaveToSVG();

            //獲取列隊中字節數組生成SVG文件
            int len = svgBytes.Count;
            for (int i = 0; i < len; i++)
            {
                FileStream fs = new FileStream(string.Format("圖片-{0}.svg", i), FileMode.Create);
                byte[] bytes = svgBytes.Dequeue();
                fs.Write(bytes, 0, bytes.Length);
                presentation.Dispose();
            }
        }
    }
}

VB.NET

Imports System.Collections.Generic
Imports System.IO
Namespace PPTtoSVG
    Class Program
        Shared  Sub Main(ByVal args() As String)
            '初始化Presentation實例
            Dim presentation As Presentation =  New Presentation() 
 
            '加載一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx")
 
            '將PowerPoint轉換為SVG圖像并以字節形式存儲在列隊中
            Dim svgBytes()> As Queue<byte =  presentation.SaveToSVG() 
 
            '獲取列隊中字節數組生成SVG文件
            Dim len As Integer =  svgBytes.Count 
            Dim i As Integer
            For  i = 0 To  len- 1  Step  i + 1
                Dim fs As FileStream =  New FileStream(String.Format("圖片-{0}.svg",i),FileMode.Create) 
                Dim bytes() As Byte =  svgBytes.Dequeue() 
                fs.Write(bytes, 0, bytes.Length)
                presentation.Dispose()
            Next
        End Sub
    End Class
End Namespace

效果圖

原文鏈接:https://www.cnblogs.com/Gia-/p/16539060.html

欄目分類
最近更新