プレゼンテーションで画像を扱う — Aspose.Slides FOSS for .NET

Aspose.Slides FOSS for .NET lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame シェイプ。画像はシェイプの背景塗りつぶしとしても使用できます。 FillType.Picture.


ファイルから画像を追加する

ディスクから画像バイトを読み込み、プレゼンテーションの画像コレクションに追加します。 prs.Images.AddImage().次に、画像をスライド上に配置します。 PictureFrame:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
// Add the image to the shared collection
var imageData = File.ReadAllBytes("logo.png");
var img = prs.Images.AddImage(imageData);

// Place it on the slide as a PictureFrame
var slide = prs.Slides[0];
slide.Shapes.AddPictureFrame(ShapeType.Rectangle, 50, 50, 300, 200, img);

prs.Save("with-image.pptx", SaveFormat.Pptx);

次の4つの位置引数は AddPictureFrame() です:: x, y, width, height ポイント単位です。.


ストリームから画像を追加する

ストリームから画像データがある場合(例:URLからダウンロードしたものやデータベースから読み取ったもの)、直接…に渡します。 AddImage():

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
using var stream = File.OpenRead("photo.jpg");
var img = prs.Images.AddImage(stream);
prs.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 100, 80, 400, 250, img);
prs.Save("from-stream.pptx", SaveFormat.Pptx);

PictureFrame の位置決めとサイズ設定

この PictureFrame が返す AddPictureFrame() はすべてを継承します Shape ジオメトリプロパティを継承し、作成後に再配置できます::

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var imageData = File.ReadAllBytes("photo.jpg");
var img = prs.Images.AddImage(imageData);

var pf = prs.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 0, 0, 100, 100, img);

// Reposition and resize after creation
pf.X = 50;
pf.Y = 100;
pf.Width = 350;
pf.Height = 250;

prs.Save("positioned.pptx", SaveFormat.Pptx);

画像をシェイプのフィルとして使用する

任意のシェイプ(PictureFrame に限らず)は、画像を背景の塗りとして使用できます。Set FillType = FillType.Picture そして画像を割り当てます PictureFillFormat.Picture.Image:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var imageData = File.ReadAllBytes("background.png");
var img = prs.Images.AddImage(imageData);

var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.RoundCornerRectangle, 50, 50, 400, 250);
shape.FillFormat.FillType = FillType.Picture;
shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
shape.FillFormat.PictureFillFormat.Picture.Image = img;

prs.Save("picture-fill.pptx", SaveFormat.Pptx);

PictureFillMode.Stretch 画像をスケールしてシェイプ全体を埋めます。.


スライド間で複数の画像を追加する

画像が追加された prs.Images はすべてのスライドで共有されます。同じ画像オブジェクトは、データを複製せずに複数のスライドで使用できます::

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var logoData = File.ReadAllBytes("logo.png");
var logo = prs.Images.AddImage(logoData);

// Add the same image to the first slide
prs.Slides[0].Shapes.AddPictureFrame(ShapeType.Rectangle, 600, 10, 100, 40, logo);

prs.Save("shared-image.pptx", SaveFormat.Pptx);

関連項目

 日本語