0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#/.NETでPowerPointスライドから画像を抽出する

Posted at

複数のPowerPointプレゼンテーションで同じ画像を使用する必要がある場合は、元のPowerPointファイルから直接画像を抽出し、保存すると、検索とダウンロードの繰り返しを避けることができます。さらに、PowerPointのスライドから重要な画像を抽出すると、元のファイルが破損したり紛失した場合のバックアップとして使用することができます。この記事では、次の2つの例を通して、PowerPointから画像を抽出するためにC#を使用する方法を紹介します。

PowerPointファイルを処理するための無料の.NETライブラリ

PowerPointの画像抽出に必要な無償ライブラリはFree Spire.Presentation for .NETです。プレゼンテーションの作成、編集、変換、保存など、PPTやPPTXドキュメントの様々な処理をサポートします。
以下のリンクからダウンロードし、手動でdllをインポートするか、Nuget経由で直接インストールしてください。

C#で特定のスライドから画像を抽出する

指定されたスライド内のピクチャを抽出するには、スライド内のすべてのシェイプを繰り返し処理し、それらが SlidePicture タイプか PictureShape タイプかを1つずつ判断し、該当する場合は対応するメソッドでそれらを抽出して保存する必要があります。手順は以下の通りです:

  1. LoadFromFile() メソッドを使用して PowerPoint ドキュメントをロードします。
  2. Presentation.Slides[index] プロパティで特定のスライドを取得します。
  3. スライド内のすべての図形を繰り返し処理します。
  4. 図形が SlidePicture 型であるかどうかを判断し、SlidePicture 型であれば、SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() メソッドで画像を抽出して保存します。
  5. 図形が PictureShape 型かどうかを判断し、PictureShape 型であれば、画像を抽出して PictureShape.EmbedImage.Image.Save() メソッドで保存します。

C#コード:

using Spire.Presentation;

namespace ExtractSlideImages
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // PowerPointプレゼンテーションを読み込む
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Input.pptx");

            // 2枚目のスライド
            ISlide slide = ppt.Slides[1];

            int i = 0;
            //スライド上のすべての図形を反復処理する
            foreach (IShape s in slide.Shapes)
            {
                // シェイプがSlidePictureタイプかどうかをチェックする
                if (s is SlidePicture)
                {
                    // 画像を取り出す
                    SlidePicture ps = s as SlidePicture;
                    ps.PictureFill.Picture.EmbedImage.Image.Save(string.Format(@"Slide\Images{0}.png", i));
                    i++;
                }
                // シェイプがPictureShapeタイプかどうかをチェックする。
                if (s is PictureShape)
                {
                    // 画像を取り出す
                    PictureShape ps = s as PictureShape;
                    ps.EmbedImage.Image.Save(string.Format(@"Output/slide_{0}.png", i));
                    i++;
                }
            }
        }
    }
}

Extract image from slide.png

C#でPowerPointファイルからすべての画像を抽出する

PowerPoint文書内のすべての画像を一度に抽出する操作は比較的簡単です。以下の手順を参考にしてください:

  1. LoadFromFile() メソッドを使用して PowerPoint ドキュメントをロードします。
  2. Presentation.Images プロパティを使用して、PowerPoint ドキュメント内のすべての画像のコレクションを取得します。
  3. 画像のコレクションを繰り返し処理し、IImageData.Image.Save() メソッドを使用して各画像を指定されたファイル・パスに保存します。

C#コード:

using Spire.Presentation;
using Spire.Presentation.Collections;
using System.Drawing;

namespace ExtractImagesFromPresentation
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // PowerPointプレゼンテーションを読み込む
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Input.pptx");

            // プレゼンテーションの画像を取得する
            ImageCollection imageCollection = ppt.Images;

            // 各画像を反復処理する
            for (int i = 0; i < imageCollection.Count; i++)
            {
                // 画像を抽出する
                imageCollection[i].Image.Save(string.Format(@"ExtractImage/Images_{0}.png", i));
            }

            ppt.Dispose();
        }
    }
}

Extract image from powerpoint.png

上記のコード例を使えば、PowerPointプレゼンテーションからの画像抽出を自動化するスクリプトを書くことができ、ビジュアル・リソースをより適切に管理・活用することができる。


C#/ .NETでPowerPointファイルを操作するその他の例は、以下を参照されたい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?