フッターコンテンツにスキップ
PPTツール

C#を使用してPowerPointを画像に変換する方法

ソフトウェア開発の分野では、 PowerPointプレゼンテーションを画像形式に変換する必要が頻繁に発生します。 多くの開発者は、プレビューの生成、サムネイルの作成、システム統合など、PowerPoint ファイルをプログラムで写真に変換できることが便利だと感じています。 この記事では、C# ppt から画像への操作を実行する方法について説明し、手順を説明するサンプル コードもいくつか示します。

C#を使ってPowerPointを画像に変換する方法

  1. PowerPoint アプリケーション インスタンスを作成します。
  2. インスタンスを使用してプレゼンテーションを開きます。
  3. 出力フォルダーを確認して作成します。
  4. スライドを反復処理し、スライドを画像にエクスポートします。
  5. プレゼンテーションを閉じてアプリケーションを終了します。

PowerPoint プレゼンテーションを画像形式に変換しますか?

具体的な内容に入る前に、PowerPoint スライドを写真に変換することの重要性について簡単に見てみましょう。 PowerPoint はダイナミックなプレゼンテーションを作成するための優れたツールですが、これらのファイルを元の形式で共有することが必ずしも可能とは限りません。 場合によっては、プレゼンテーションから取得した特定のスライドや写真だけが必要な場合があり、また、システムや設定の違いにより PowerPoint ファイルを直接レンダリングできない場合もあります。 PowerPoint プレゼンテーションを画像に変換することで、さまざまなデバイスやアプリケーションで簡単に共有および表示できる包括的なソリューションが提供されます。

PowerPoint Interopライブラリの使用

C# で PowerPoint プレゼンテーションを写真に変換する方法はいくつかあります。 PowerPoint アプリケーションとプログラム的にインターフェイスするためのクラスとメソッドを提供するMicrosoft.Office.Interop.PowerPoint 名前空間を使用するのが、一般的なアプローチの 1 つです。 これにより、PowerPoint ファイルの操作に広範な機能が提供されます。

新しいVisual Studioプロジェクトを作成

新しい Visual Studio プロジェクトを作成するには、以下の手順に従います。

Visual Studio IDEを開きます。使用する前に、PCにVisual Studioがインストールされていることを確認してください。

新しいプロジェクトを開始する:

ファイルを選択し、新規、プロジェクトを選択します。

! C# を使用して PowerPoint を画像に変換する方法: 図 1 - Visual Studio を開き、[ファイル] - [新規作成] - [プロジェクト] を選択します。

"新しいプロジェクトの作成"ボックスで、左側からお気に入りのプログラミング言語 (たとえば、C#) を選択します。

次に、利用可能なプロジェクト テンプレートのリストから"コンソール アプリ"または"コンソール アプリ (.NET Core)"テンプレートを選択します。

プロジェクトに名前を付けるために、"Name"セクションを完成させてください。

C#を使ってPowerPointを画像に変換する方法:図2 - "新しいプロジェクトの作成"ボックスから、プログラミング言語としてC#を選択し、"コンソールアプリ"を選択します。プロジェクト名と場所を設定し、"次へ"ボタンをクリックします。

プロジェクトの保存場所を選択します。

"作成"をクリックして、新しいコンソール アプリケーション プロジェクトの作業を開始します。

! C# を使用して PowerPoint を画像に変換する方法: 図 3 - 適切な .NET Framework を選択し、"作成"ボタンをクリックします。

C# で PowerPoint スライドを画像に変換する

まず、Microsoft.Office.Interop.PowerPoint 名前空間を使用して PowerPoint スライドを画像に変換する方法を見てみましょう。 まず、必要なアセンブリがインストールされ、C#プロジェクトに参照として追加されていることを確認してください。これらのアセンブリは通常、InterOpアセンブリを直接参照するか、Microsoft Office Primary Interop Assemblies (PIA) をインストールすることで見つかります。

コード例

using System.IO; // Import System.IO namespace for file handling
using Microsoft.Office.Interop.PowerPoint; // Import Interop PowerPoint namespace

class Program
{
    static void Main(string[] args)
    {
        string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
        string outputFolder = "output_images"; // Output folder path where images will be saved

        ConvertPptToImages(pptFilePath, outputFolder); // Convert PowerPoint slides to images
    }

    static void ConvertPptToImages(string pptFilePath, string outputFolder)
    {
        Application pptApplication = new Application(); // Create a new PowerPoint application instance
        Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); // Open the PowerPoint presentation

        if (!Directory.Exists(outputFolder)) // Check if the output folder exists
            Directory.CreateDirectory(outputFolder); // Create the output folder if it doesn't exist

        int slidesCount = pptPresentation.Slides.Count; // Get the number of slides in the presentation

        for (int i = 1; i <= slidesCount; i++) // Iterate through all slides
        {
            string outputPath = Path.Combine(outputFolder, $"Slide{i}.png"); // Set the output path for the current slide
            pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768); // Export slide to PNG format
        }

        pptPresentation.Close(); // Close the PowerPoint presentation
        pptApplication.Quit(); // Quit the PowerPoint application
    }
}
using System.IO; // Import System.IO namespace for file handling
using Microsoft.Office.Interop.PowerPoint; // Import Interop PowerPoint namespace

class Program
{
    static void Main(string[] args)
    {
        string pptFilePath = "demo.pptx"; // Path to your PowerPoint file
        string outputFolder = "output_images"; // Output folder path where images will be saved

        ConvertPptToImages(pptFilePath, outputFolder); // Convert PowerPoint slides to images
    }

    static void ConvertPptToImages(string pptFilePath, string outputFolder)
    {
        Application pptApplication = new Application(); // Create a new PowerPoint application instance
        Presentation pptPresentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse); // Open the PowerPoint presentation

        if (!Directory.Exists(outputFolder)) // Check if the output folder exists
            Directory.CreateDirectory(outputFolder); // Create the output folder if it doesn't exist

        int slidesCount = pptPresentation.Slides.Count; // Get the number of slides in the presentation

        for (int i = 1; i <= slidesCount; i++) // Iterate through all slides
        {
            string outputPath = Path.Combine(outputFolder, $"Slide{i}.png"); // Set the output path for the current slide
            pptPresentation.Slides[i].Export(outputPath, "png", 1024, 768); // Export slide to PNG format
        }

        pptPresentation.Close(); // Close the PowerPoint presentation
        pptApplication.Quit(); // Quit the PowerPoint application
    }
}
Imports System.IO ' Import System.IO namespace for file handling
Imports Microsoft.Office.Interop.PowerPoint ' Import Interop PowerPoint namespace

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim pptFilePath As String = "demo.pptx" ' Path to your PowerPoint file
		Dim outputFolder As String = "output_images" ' Output folder path where images will be saved

		ConvertPptToImages(pptFilePath, outputFolder) ' Convert PowerPoint slides to images
	End Sub

	Private Shared Sub ConvertPptToImages(ByVal pptFilePath As String, ByVal outputFolder As String)
		Dim pptApplication As New Application() ' Create a new PowerPoint application instance
		Dim pptPresentation As Presentation = pptApplication.Presentations.Open(pptFilePath, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse) ' Open the PowerPoint presentation

		If Not Directory.Exists(outputFolder) Then ' Check if the output folder exists
			Directory.CreateDirectory(outputFolder) ' Create the output folder if it doesn't exist
		End If

		Dim slidesCount As Integer = pptPresentation.Slides.Count ' Get the number of slides in the presentation

		For i As Integer = 1 To slidesCount ' Iterate through all slides
			Dim outputPath As String = Path.Combine(outputFolder, $"Slide{i}.png") ' Set the output path for the current slide
			pptPresentation.Slides(i).Export(outputPath, "png", 1024, 768) ' Export slide to PNG format
		Next i

		pptPresentation.Close() ' Close the PowerPoint presentation
		pptApplication.Quit() ' Quit the PowerPoint application
	End Sub
End Class
$vbLabelText   $csharpLabel

PowerPoint アプリを操作するために必要な C# 名前空間はMicrosoft.Office.Interop.PowerPoint;宣言を使用してインポートされます。 プログラムの入り口はMainメソッドです。 作成された写真が保存される出力フォルダー ( outputFolder ) と、PowerPoint ファイルへのパス ( pptFilePath ) を指定します。 PowerPoint プレゼンテーションを写真に変換する実際の作業は、この方法で処理されます。

PowerPointプレゼンテーションファイル

! C# を使用して PowerPoint を画像に変換する方法: 図 4 - コード例に使用される PowerPoint ppt。

Application pptApplication = new Application();は、PowerPoint プログラムの新しいインスタンスを開始するために使用されます。 これにより、PowerPoint とのプログラムによる対話が可能になります。 pptApplication.Presentationsを使用して、 pptFilePath.Open()関数によって示された PowerPoint プレゼンテーション ファイルを開きます。 この関数は、開かれたプレゼンテーションを表す Presentation オブジェクトを返します。 出力フォルダ" outputFolder "が存在するかどうかを判断します。 そうでない場合は、メソッドDirectory.CreateDirectory()を使用して作成します。

コンソール出力

! C# を使用して PowerPoint を画像に変換する方法: 図 5 - コンソール出力。

プレゼンテーション内の各スライドを反復処理するには、 for ループを使用します。 pptPresentation Slides.Countプロパティを使用してスライドの合計数を提供します。 出力フォルダー パスとスライド インデックスを使用して、各スライドの画像 ( Slide{i}.pngなど) の出力パスを作成します。 次に、 pptPresentation使用して、 Export()関数で PowerPoint スライドを画像 (この例では PNG 画像形式) としてエクスポートします。 パラメータは、画像フォーマット("png"フォーマット)とサイズ(幅:1024、高さ:768)です。 最後に、 pptPresentation.Close()を使用してプレゼンテーションを終了し、 pptApplication.Quit()を使用して PowerPoint セッションを終了します。 システム リソースを適切に放棄するには、 Quit()を使用します。

出力 - PowerPoint を PNG 画像に変換する

! C# を使用して PowerPoint を画像に変換する方法: 図 6 - エクスポートされた PowerPoint スライドを PNG 画像出力に出力します。

IronPPT

IronPPT は、Microsoft Office または Office Interop コンポーネントを必要とせずに、C# または VB.NET を使用して PowerPoint (PPT/PPTX) ファイルを操作するための Iron Software の専用 .NET ライブラリです。

主要機能

  • Office を必要としない PowerPoint 処理: PowerPoint をインストールせずに、任意の .NET プラットフォーム (Windows、macOS、Linux、Docker、Azure) で.pptx (および.ppt ) ファイルを読み込み、編集、または作成します。 *スライドの種類とレイアウトの*制御 (サイズ、向き、背景、マスター レイアウトなど)。 リッチ コンテンツのサポート: テキストの追加とスタイル設定 (フォント、サイズ、色、配置)、図形の描画、画像の挿入、グラフや表の構成など、すべて流暢な API で行えます。 高忠実度画像のエクスポート: Save()またはExport()メソッドを使用して、各Slideカスタム解像度で PNG または JPEG として保存できます (例: presentation.Save(&quot;Slide1.png&quot;, width:1200, height:800) )。 複数の .NET バージョン**がサポートされています: Azure またはコンテナー環境では、.NET Framework 4.6.2+、.NET Core 3.1、.NET 5 ~ 9、.NET 6/7/8 がサポートされています。 *サーバーセーフかつスレッドフレンドリー: バックグラウンド サービス、Web API、CI/CD ワークロードに最適です。

IronPPTをインストールする

次のいずれかの方法で、NuGet パッケージをプロジェクトに追加します。

Install-Package IronPPT

または Visual Studio の NuGet パッケージ マネージャー GUI 経由 ("IronPPT"を検索)。 インストール後、以下を追加してインポートします。

using IronPPT;
using IronPPT;
Imports IronPPT
$vbLabelText   $csharpLabel

すべての機能のロックを解除するには、ライセンス キーを設定するか、30 日間の無料試用キーを使用します。

IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY";
IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY";
IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY"
$vbLabelText   $csharpLabel

IronPPTでPowerPointスライドを画像に変換する

IronPPTを使えば、スライドを画像に変換するのが簡単かつ簡潔になります。その方法を示すC#の慣用的な例を以下に示します。

using IronPPT;
using System.IO;

// Optional: apply the license key
// IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY";

var presentation = PresentationDocument.Load("input.pptx");

if (!Directory.Exists("images"))
    Directory.CreateDirectory("images");

for (int i = 0; i < presentation.Slides.Count; i++)
{
    var slide = presentation.Slides[i];
    string filePath = Path.Combine("images", $"slide{i+1}.png");
    slide.SaveAsImage(filePath, width: 1024, height: 768);
}

presentation.Close();
using IronPPT;
using System.IO;

// Optional: apply the license key
// IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY";

var presentation = PresentationDocument.Load("input.pptx");

if (!Directory.Exists("images"))
    Directory.CreateDirectory("images");

for (int i = 0; i < presentation.Slides.Count; i++)
{
    var slide = presentation.Slides[i];
    string filePath = Path.Combine("images", $"slide{i+1}.png");
    slide.SaveAsImage(filePath, width: 1024, height: 768);
}

presentation.Close();
Imports IronPPT
Imports System.IO

' Optional: apply the license key
' IronPPT.License.LicenseKey = "YOUR_LICENSE_KEY";

Private presentation = PresentationDocument.Load("input.pptx")

If Not Directory.Exists("images") Then
	Directory.CreateDirectory("images")
End If

For i As Integer = 0 To presentation.Slides.Count - 1
	Dim slide = presentation.Slides(i)
	Dim filePath As String = Path.Combine("images", $"slide{i+1}.png")
	slide.SaveAsImage(filePath, width:= 1024, height:= 768)
Next i

presentation.Close()
$vbLabelText   $csharpLabel

このアプローチでは COM が完全に回避されます。 IronPPT はページ付け、ベクターのスケーリング、画像のレンダリングを内部で処理するため、画像は PowerPoint の外観と感覚と一致します。

より高度な使用方法としては、スライドの順序を制御したり、テンプレートを再利用したり、表やグラフを追加したり、カスタム SVG/ベクター画像を挿入したりできます (完全なクラスとメソッドの内訳については、詳細な APIリファレンスを参照してください)。

結論

多くの最新の .NET アプリケーションでは、ドキュメントのプレビュー、自動レポート、または下流の処理のために、PowerPoint プレゼンテーションを画像に変換することが不可欠です。 このタスクには Microsoft Office Interop コンポーネントを使用できますが、Office のインストール、安定性の問題、ライセンスに関する懸念、プラットフォームの制約など、多くの制限が生じます。

代わりに、IronPPTは、.pptxファイルを画像に変換するための、フル機能、高パフォーマンス、クロスプラットフォームのAPIを提供しています。単一のスライドからデッキ全体まで、あらゆるファイル形式に対応しています。デスクトップクライアント、Web API、ヘッドレスサーバー環境のいずれであっても、IronPPTはOfficeを必要とせずに、同等の忠実度と制御性を実現します。上記の相互運用性ベースのコードをIronPPTに置き換えれば、より高速かつ確実に、そして.NETの完全な制御下でPowerPointプレビューを生成できるようになります。

さらに多くの機能を調べるには、IronPPT API リファレンスにアクセスするか、詳細なコード例 ( llms.txtインデックスを含む) を参照してください。 無料トライアルをご利用いただけます。今すぐ試して、PowerPoint から画像への変換機能を .NET ツールキットに追加しましょう。

Jordi Bardia
ソフトウェアエンジニア
Jordiは、最も得意な言語がPython、C#、C++であり、Iron Softwareでそのスキルを発揮していない時は、ゲームプログラミングをしています。製品テスト、製品開発、研究の責任を分担し、Jordiは継続的な製品改善において多大な価値を追加しています。この多様な経験は彼を挑戦させ続け、興味を持たせており、Iron Softwareで働くことの好きな側面の一つだと言います。Jordiはフロリダ州マイアミで育ち、フロリダ大学でコンピュータサイエンスと統計学を学びました。