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

How to Use C# to Convert PowerPoint to Image

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

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

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

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

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

PowerPoint Interopライブラリの使用

C# で PowerPoint プレゼンテーションを写真に変換する方法はいくつかあります。 [Microsoft.Office.Interop.PowerPoint](https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff761925(v=office.12)名前空間を使用して、PowerPointアプリケーションとプログラム的にインターフェイスするクラスとメソッドを提供するアプローチが一般的です。 これにより、PowerPoint ファイルの操作に広範な機能が提供されます。

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

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

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

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

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

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

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

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

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

How to Use C# to Convert PowerPoint to Image: Figure 2 - From the Create New Project box, select the C# programming language and Console App. Configure the Project name and location, then click on the Next button.

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

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

How to Use C# to Convert PowerPoint to Image: Figure 3 - Select the appropriate .NET Framework and click on the Create button.

Convert PowerPoint Slides to Images in C

まず、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#のnamespaceは、Microsoft.Office.Interop.PowerPoint;宣言を使用してインポートされます。 プログラムのエントリーポイントはMainメソッドです。 出力フォルダー(pptFilePath)を指定します。 PowerPoint プレゼンテーションを写真に変換する実際の作業は、この方法で処理されます。

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

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

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

コンソール出力

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

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

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

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

IronPPT

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

主要機能

*Officeを使用しないPowerPoint処理:*任意 for .NETプラットフォーム上で.ppt)ファイルをロード、編集、作成します。Windows、macOS、Linux、Docker、またはAzureにPowerPointをインストールせずに対応可能です。 スライドの種類とレイアウトの制御 (サイズ、向き、背景、マスター レイアウトなど)。 豊富なコンテンツのサポート:テキストの追加とスタイル設定(フォント、サイズ、色、配置)、図形の描画、画像の挿入、グラフや表の設定など、すべて流暢なAPIで行えます。 忠実度の高い画像エクスポート:presentation.Save("Slide1.png", 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 ツールキットに追加しましょう。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

アイアンサポートチーム

私たちは週5日、24時間オンラインで対応しています。
チャット
メール
電話してね