.NET で PowerPoint の図形をフォーマットする

概要

PowerPoint で図形の輪郭、塗りつぶし、効果を調整して図形をフォーマットします。Aspose.Slides for .NET を使用すると、透明度、回転、3D 書式設定などの設定をプログラムで制御できます。

PowerPoint の図形書式設定オプション

Aspose.Slides for .NET は、PowerPoint で利用できるのと同じオプションを使用して図形をフォーマットできるインターフェイスとプロパティを提供します。

線の書式設定

Use shape の輪郭 to control a shape’s outline. This example creates an unfilled rectangle with a blue, dashed outline. スタイル selects a thick‑thin compound line, while 幅 sets its width to 7 points and ダッシュスタイル selects the dash pattern.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.Style = LineStyle.ThickThin;
shape.LineFormat.Width = 7;
shape.LineFormat.DashStyle = LineDashStyle.Dash;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;

presentation.Save("formatted_lines.pptx", SaveFormat.Pptx);

結果:

プレゼンテーション内の書式設定された線

カスタム線ダッシュ パターンの設定

Use カスタム ダッシュ パターン when predefined line styles do not provide the dash lengths and gaps you need, for example for branded outlines or technical diagrams. Assigning a pattern automatically sets ダッシュスタイル to LineDashStyle.Custom.

配列のエントリはダッシュとギャップの長さが交互に続き、最初はダッシュから始まります。各値は線幅の倍数です。{ 4, 2 } を幅 3 pt で使用すると、12 pt のダッシュの後に 6 pt のギャップができます。有限かつ非負の値のペアで完全なダッシュ‑ギャップ セグメントを定義します。例ではすべてのダッシュとギャップが見えるように正の値を使用しています。

この例では、同じ長さでフラットなキャップを持つ 5 本の青い線を作成します。最初の 3 本は幅 3 pt のパターンを比較し、4 本目は最初のパターンを幅 2 倍にしてダッシュとギャップの長さを倍にします。5 本目はカスタムパターンから実線に切り替えます。

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(600, 300, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var patterns = new[]
{
    new float[] { 4, 2 },
    new float[] { 1, 2 },
    new float[] { 6, 2, 1, 2 },
    new float[] { 4, 2 },
    new float[] { 4, 2 }
};

for (var i = 0; i < patterns.Length; i++)
{
    var line = slide.Shapes.AddAutoShape(ShapeType.Line, 50, 50 + i * 50, 500, 0);
    line.LineFormat.Width = i == 3 ? 6 : 3;
    line.LineFormat.FillFormat.FillType = FillType.Solid;
    line.LineFormat.FillFormat.SolidFillColor.Color = Color.DarkBlue;
    line.LineFormat.CapStyle = LineCapStyle.Flat;
    line.LineFormat.BeginArrowheadStyle = LineArrowheadStyle.None;
    line.LineFormat.EndArrowheadStyle = LineArrowheadStyle.None;
    line.LineFormat.CustomDashPattern = patterns[i];
}

// Return the last line to a solid style.
slide.Shapes[4].LineFormat.DashStyle = LineDashStyle.Solid;

presentation.Save("custom_dash_patterns.pptx", SaveFormat.Pptx);

To return to a predefined dashed style instead, assign LineDashStyle.Dash to ILineFormat.DashStyle. The selected style controls the line’s appearance.

PowerPoint の結果(上から下へ):{4, 2}, 3 pt; {1, 2}, 3 pt; {6, 2, 1, 2}, 3 pt; {4, 2}, 6 pt; and Solid, 3 pt.

PowerPoint の 3 つのカスタムダッシュパターン、最初のパターンを使用した太い線、そして実線

線端の設定

Use ILineFormat.CapStyle to set the ends of an open line. The LineCapStyle enumeration provides three explicit cap styles:

  • Flat: The stroke stops at the endpoint.
  • Round: A semicircular cap extends beyond the endpoint by half the line width.
  • Square: A square cap extends beyond the endpoint by half the line width.

The enumeration also includes NotDefined, which means that no cap style is explicitly assigned.

Caps are different from arrowheads, which add symbols to line ends, and joins, which control corners between line segments. This example disables arrowheads through ILineFormat.BeginArrowheadStyle and ILineFormat.EndArrowheadStyle so that only the cap shapes are compared.

The following example creates three otherwise identical horizontal lines, 20 points wide. From top to bottom, they use flat, round, and square caps. Their endpoints have the same horizontal coordinates; the round and square caps extend 10 points beyond each endpoint.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(500, 240, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var capStyles = new[] { LineCapStyle.Flat, LineCapStyle.Round, LineCapStyle.Square };

for (var i = 0; i < capStyles.Length; i++)
{
    var line = slide.Shapes.AddAutoShape(ShapeType.Line, 100, 50 + i * 70, 300, 0);
    line.LineFormat.Width = 20;
    line.LineFormat.FillFormat.FillType = FillType.Solid;
    line.LineFormat.FillFormat.SolidFillColor.Color = Color.DarkBlue;
    line.LineFormat.DashStyle = LineDashStyle.Solid;
    line.LineFormat.CapStyle = capStyles[i];
    line.LineFormat.BeginArrowheadStyle = LineArrowheadStyle.None;
    line.LineFormat.EndArrowheadStyle = LineArrowheadStyle.None;
}

presentation.Save("line_caps.pptx", SaveFormat.Pptx);

PowerPoint の結果(上から下へ):Flat、Round、Square。

PowerPoint の同一太線上のフラット、ラウンド、スクエアのキャップ

図形線へのスケッチ効果の適用

A sketch effect makes a shape line look hand‑drawn. Use IShape.LineFormat to access the line settings, ILineFormat.SketchFormat to access the sketch settings, and ISketchFormat.SketchType to select a value from the LineSketchType enumeration.

The following C# code shows how to apply a LineSketchType.Curved effect, read the explicitly assigned value, and remove the effect with LineSketchType.None:

using System;
using Aspose.Slides;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);

var sketchFormat = shape.LineFormat.SketchFormat;
sketchFormat.SketchType = LineSketchType.Curved;
var explicitSketchType = sketchFormat.SketchType;
Console.WriteLine($"Explicit sketch type: {explicitSketchType}");
sketchFormat.SketchType = LineSketchType.None;

The value returned by ISketchFormat.SketchType represents the setting assigned directly to the shape. If the line formatting can be inherited from a theme, master slide, or layout slide, use ILineFormat.GetEffective, access ILineFormatEffectiveData.SketchFormat, and read ISketchFormatEffectiveData.SketchType. The effective value reflects the formatting that is actually applied after inheritance is resolved. This example requires an existing presentation with at least one shape on its first slide and prints both values:

using System;
using Aspose.Slides;

using var presentation = new Presentation("presentation.pptx");
var shape = presentation.Slides[0].Shapes[0];

var lineFormat = shape.LineFormat;
var explicitSketchType = lineFormat.SketchFormat.SketchType;
var effectiveLineFormat = lineFormat.GetEffective();
var effectiveSketchType = effectiveLineFormat.SketchFormat.SketchType;

Console.WriteLine($"Explicit sketch type: {explicitSketchType}");
Console.WriteLine($"Effective sketch type: {effectiveSketchType}");

結合スタイルの書式設定

次の 3 つの結合タイプオプションがあります:

  • Round
  • Miter
  • Bevel

The join style controls how two outline segments meet at a corner. Use Round for rounded corners, Miter for sharp corners, or Bevel for flattened corners.

プレゼンテーション内の結合スタイル

This example creates three black rectangles with blue outlines 15 points wide. It sets ILineFormat.JoinStyle to the miter, bevel, and round values of LineJoinStyle, respectively, to compare how the corners are drawn.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var miterShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 150, 75);
var bevelShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 210, 20, 150, 75);
var roundShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 135, 150, 75);

miterShape.FillFormat.FillType = FillType.Solid;
miterShape.FillFormat.SolidFillColor.Color = Color.Black;
bevelShape.FillFormat.FillType = FillType.Solid;
bevelShape.FillFormat.SolidFillColor.Color = Color.Black;
roundShape.FillFormat.FillType = FillType.Solid;
roundShape.FillFormat.SolidFillColor.Color = Color.Black;

miterShape.LineFormat.Width = 15;
bevelShape.LineFormat.Width = 15;
roundShape.LineFormat.Width = 15;

miterShape.LineFormat.FillFormat.FillType = FillType.Solid;
miterShape.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;
bevelShape.LineFormat.FillFormat.FillType = FillType.Solid;
bevelShape.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;
roundShape.LineFormat.FillFormat.FillType = FillType.Solid;
roundShape.LineFormat.FillFormat.SolidFillColor.Color = Color.Blue;

miterShape.LineFormat.JoinStyle = LineJoinStyle.Miter;
bevelShape.LineFormat.JoinStyle = LineJoinStyle.Bevel;
roundShape.LineFormat.JoinStyle = LineJoinStyle.Round;

miterShape.TextFrame.Text = "Miter Join Style";
bevelShape.TextFrame.Text = "Bevel Join Style";
roundShape.TextFrame.Text = "Round Join Style";

presentation.Save("join_styles.pptx", SaveFormat.Pptx);

鋭角でのミーター結合の制限

Use ILineFormat.MiterLimit with ILineFormat.JoinStyle set to LineJoinStyle.Miter to limit how far a sharp corner can extend.

The limit is a ratio of the distance between the inner and outer corners of the join to the line width. For example, a value of 8 permits a miter length of up to eight times the line width; it is not a measurement in points. If the corner would exceed the limit, PowerPoint uses a beveled join at that corner. A larger limit allows sharper points. The setting affects miter joins only, and changing it has no visible effect when the corner remains within both limits.

This example creates two identical, unfilled triangles with outlines 20 points wide. The left triangle uses a limit of 2, and the right uses 8. Each triangle is 80 points wide and 160 points high, giving its top corner a miter‑length‑to‑line‑width ratio of approximately 4.12. Consequently, PowerPoint bevels the left apex and preserves the right apex.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(500, 320, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var miterLimits = new[] { 2f, 8f };

for (var i = 0; i < miterLimits.Length; i++)
{
    var triangle = slide.Shapes.AddAutoShape(ShapeType.Triangle, 90 + i * 240, 110, 80, 160);
    triangle.FillFormat.FillType = FillType.NoFill;
    triangle.LineFormat.Width = 20;
    triangle.LineFormat.FillFormat.FillType = FillType.Solid;
    triangle.LineFormat.FillFormat.SolidFillColor.Color = Color.DarkBlue;
    triangle.LineFormat.JoinStyle = LineJoinStyle.Miter;
    triangle.LineFormat.MiterLimit = miterLimits[i];
}

presentation.Save("miter_limits.pptx", SaveFormat.Pptx);

PowerPoint の結果:左は Miter limit 2、右は miter limit 8。

PowerPoint のミーターリミット 2 の面取り頂点とミーターリミット 8 の尖った頂点

グラデーション塗りつぶし

A gradient fill blends colors across a shape. This example fills an ellipse with a linear gradient at a 45‑degree angle from red to purple using IFillFormat.GradientFormat. The IGradientStopCollection.Add method places the color stops at 0 and 1, the beginning and end of the gradient. Clearing the existing stops ensures that only these two colors are used.

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

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Ellipse, 50, 50, 150, 75);
shape.FillFormat.FillType = FillType.Gradient;
shape.FillFormat.GradientFormat.GradientShape = GradientShape.Linear;
shape.FillFormat.GradientFormat.LinearGradientAngle = 45;
shape.FillFormat.GradientFormat.GradientStops.Clear();
shape.FillFormat.GradientFormat.GradientStops.Add(1.0f, PresetColor.Purple);
shape.FillFormat.GradientFormat.GradientStops.Add(0.0f, PresetColor.Red);

presentation.Save("gradient_fill.pptx", SaveFormat.Pptx);

結果:

グラデーション塗りつぶしの楕円

If a gradient looks different in an exported image than it does in PowerPoint, see グラデーション描画スタイルの選択. You can select a rendering style without changing the gradient fill stored in the presentation.

線形グラデーション角度の設定

Use IGradientFormat.LinearGradientAngle to control the orientation of a GradientShape.Linear fill. The angle is measured in degrees and increases clockwise: 0 runs from left to right, 90 from top to bottom, 180 from right to left, and 270 from bottom to top. Fractional angles are supported. Finite angles outside [0, 360) are normalized to one revolution; for example, -45 becomes 315 and 360 becomes 0.

This example creates six rectangles with identical red, green, and blue stops at positions 0, 0.5, and 1. Scaling is disabled so that the angle can be compared independently of the rectangles' proportions. The angles, from left to right, are 0, 45, and 90 in the first row, and 135, 180, and 270 in the second row.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(720, 400, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var angles = new float[] { 0, 45, 90, 135, 180, 270 };

for (var i = 0; i < angles.Length; i++)
{
    var x = 30 + (i % 3) * 230;
    var y = 30 + (i / 3) * 190;

    var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, x, y, 200, 150);
    shape.LineFormat.FillFormat.FillType = FillType.NoFill;
    shape.FillFormat.FillType = FillType.Gradient;

    var gradient = shape.FillFormat.GradientFormat;
    gradient.GradientShape = GradientShape.Linear;
    gradient.GradientStops.Clear();
    gradient.GradientStops.Add(0f, Color.Red);
    gradient.GradientStops.Add(0.5f, Color.Lime);
    gradient.GradientStops.Add(1f, Color.Blue);
    gradient.LinearGradientScaled = NullableBool.False;
    gradient.LinearGradientAngle = angles[i];
}

presentation.Save("linear_gradient_angles.pptx", SaveFormat.Pptx);

PowerPoint の結果:

上段の 0°, 45°, 90°、下段の 135°, 180°, 270° の線形グラデーション

形状で線形グラデーションをスケーリング

IGradientFormat.LinearGradientScaled accepts a NullableBool value. Set it explicitly to False to keep the geometric gradient angle as the shape’s proportions change, or to True to scale the gradient with the shape’s width and height. With scaling enabled, the visible angle can differ from the assigned angle on a non‑square shape.

The following example uses a 45‑degree angle and the same color stops as the angle comparison. The left column uses False; the right column uses True. Each column contains a wide rectangle above a tall rectangle. Both start with the same wide dimensions; changing the second rectangle’s width and height demonstrates how the fill responds to resizing without changing its gradient settings.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(720, 460, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

var scalingOptions = new[] { NullableBool.False, NullableBool.True };

for (var column = 0; column < scalingOptions.Length; column++)
{
    for (var row = 0; row < 2; row++)
    {
        var x = 30 + column * 360;
        var y = row == 0 ? 30 : 150;
        
        var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, x, y, 300, 90);
        shape.LineFormat.FillFormat.FillType = FillType.NoFill;
        shape.FillFormat.FillType = FillType.Gradient;

        var gradient = shape.FillFormat.GradientFormat;
        gradient.GradientShape = GradientShape.Linear;
        gradient.GradientStops.Clear();
        gradient.GradientStops.Add(0f, Color.Red);
        gradient.GradientStops.Add(0.5f, Color.Lime);
        gradient.GradientStops.Add(1f, Color.Blue);
        gradient.LinearGradientAngle = 45;
        gradient.LinearGradientScaled = scalingOptions[column];

        if (row == 1)
        {
            shape.Width = 90;
            shape.Height = 280;
        }
    }
}

presentation.Save("linear_gradient_scaling.pptx", SaveFormat.Pptx);

PowerPoint の結果(左はスケーリング無効、右は有効):

45° のグラデーションを持つ横長と縦長の矩形、左は LinearGradientScaled が False、右は True の比較

スケーリングを無効にすると、対角線状の色帯は両方の矩形で同じ向きのままです。スケーリングを有効にすると、矩形の比率に合わせて帯が伸びます。これにより、テンプレートのグラデーションを別サイズの形状に適用するときに同一の外観を再現できます。正方形の場合、スケーリングを有効にしてもこの 45° のグラデーションの外観は変わりません。

Both the angle and scaling settings apply to linear gradients. They do not control GradientShape.Radial or GradientShape.Rectangle fills.

角度とグラデーション方向の組み合わせ

IGradientFormat.GradientDirection selects a gradient style from the GradientDirection enumeration; it is not a substitute for a numeric angle. For a linear gradient, the corner styles can change the area over which the colors are distributed without changing LinearGradientAngle. Keep the direction unchanged when comparing angles or scaling.

The following example applies the same settings in opposite orders. The two rectangles have the same appearance: assigning the direction before or after the angle does not change the result.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

for (var i = 0; i < 2; i++)
{
    var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 30, 30 + i * 140, 320, 100);
    shape.LineFormat.FillFormat.FillType = FillType.NoFill;
    shape.FillFormat.FillType = FillType.Gradient;

    var gradient = shape.FillFormat.GradientFormat;
    gradient.GradientShape = GradientShape.Linear;
    gradient.GradientStops.Clear();
    gradient.GradientStops.Add(0f, Color.Red);
    gradient.GradientStops.Add(0.5f, Color.Lime);
    gradient.GradientStops.Add(1f, Color.Blue);
    gradient.LinearGradientScaled = NullableBool.True;

    if (i == 0)
    {
        gradient.GradientDirection = GradientDirection.FromCorner1;
        gradient.LinearGradientAngle = 30;
    }
    else
    {
        gradient.LinearGradientAngle = 30;
        gradient.GradientDirection = GradientDirection.FromCorner1;
    }
}

presentation.Save("linear_gradient_direction.pptx", SaveFormat.Pptx);

パターン塗りつぶし

A pattern fill applies a two‑color design, such as dots, stripes, or checks. This example uses IPatternFormat.PatternStyle to select a trellis pattern. IPatternFormat.BackColor supplies the light gray background, and IPatternFormat.ForeColor supplies the yellow pattern color.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.FillFormat.FillType = FillType.Pattern;
shape.FillFormat.PatternFormat.PatternStyle = PatternStyle.Trellis;
shape.FillFormat.PatternFormat.BackColor.Color = Color.LightGray;
shape.FillFormat.PatternFormat.ForeColor.Color = Color.Yellow;

presentation.Save("pattern_fill.pptx", SaveFormat.Pptx);

パターン塗りつぶしの矩形

画像塗りつぶし

A picture fill uses an image inside a shape. This example requires a local image and uses Images.FromFile to load it into the presentation’s image collection. Setting IPictureFillFormat.PictureFillMode to the tiled mode repeats the image within the rectangle. The source image used here is shown below:

蓮の画像

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

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 255, 130);
shape.FillFormat.FillType = FillType.Picture;
shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Tile;

using var image = Images.FromFile("lotus.png");
var presentationImage = presentation.Images.AddImage(image);
shape.FillFormat.PictureFillFormat.Picture.Image = presentationImage;

presentation.Save("picture_fill.pptx", SaveFormat.Pptx);

画像塗りつぶしの形状

画像をテクスチャとしてタイル表示

If you want to set a tiled picture as a texture and customize the tiling behavior, you can use the following properties of the IPictureFillFormat interface and PictureFillFormat class:

  • PictureFillMode: 画像塗りつぶしモード—Tile または Stretch を設定します。
  • TileAlignment: 形状内のタイル配置を指定します。
  • TileFlip: タイルを水平、垂直、または両方に反転するか制御します。
  • TileOffsetX: 形状の原点からタイルの水平オフセット(ポイント単位)を設定します。
  • TileOffsetY: 形状の原点からタイルの垂直オフセット(ポイント単位)を設定します。
  • TileScaleX: タイルの水平スケールをパーセンテージで定義します。
  • TileScaleY: タイルの垂直スケールをパーセンテージで定義します。

This example requires a local image and configures IPictureFillFormat to repeat it at 50% of its original size. Offsets of −32 points shift the tiles horizontally and vertically; bottom‑right alignment and flipping in both directions determine how the repeated texture is arranged.

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

using var presentation = new Presentation();
var firstSlide = presentation.Slides[0];

var shape = firstSlide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 190, 95);
shape.FillFormat.FillType = FillType.Picture;

using var sourceImage = Images.FromFile("lotus.png");
var presentationImage = presentation.Images.AddImage(sourceImage);

var pictureFillFormat = shape.FillFormat.PictureFillFormat;
pictureFillFormat.Picture.Image = presentationImage;
pictureFillFormat.PictureFillMode = PictureFillMode.Tile;
pictureFillFormat.TileOffsetX = -32;
pictureFillFormat.TileOffsetY = -32;
pictureFillFormat.TileScaleX = 50;
pictureFillFormat.TileScaleY = 50;
pictureFillFormat.TileAlignment = RectangleAlignment.BottomRight;
pictureFillFormat.TileFlip = TileFlip.FlipBoth;

presentation.Save("tile.pptx", SaveFormat.Pptx);

タイル設定オプション

単色塗りつぶし

A solid fill gives a shape a single, uniform color. This example sets IFillFormat.FillType to the solid fill mode and assigns yellow through IFillFormat.SolidFillColor, then saves the rectangle in a presentation.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.FillFormat.FillType = FillType.Solid;
shape.FillFormat.SolidFillColor.Color = Color.Yellow;

presentation.Save("solid_color_fill.pptx", SaveFormat.Pptx);

単色塗りつぶしの形状

テーマ参照を保持しながら色をコピー

Use IColorFormat.CopyFrom to copy a color definition, including its color type, theme reference, and transformations. This is useful when transferring a fill color between shapes that should continue to follow the presentation theme. The method copies only the color: it does not copy the fill type, outline, or other shape formatting. Set the destination’s IFillFormat.FillType explicitly.

This example creates three rectangles, from left to right: the source, a theme‑aware copy, and a fixed RGB copy. The source uses IColorFormat.SchemeColor to reference the first accent color and adds a tint through IColorOperationCollection.Add. The RGB copy uses the source’s effective fill color so all three initially look identical. Assigning IColorFormat.Color sets a fixed RGB value and clears any existing color transformations.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(660, 220, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

presentation.MasterTheme.ColorScheme.Accent1.Color = Color.RoyalBlue;

var source = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 30, 40, 180, 140);
var themeCopy = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 240, 40, 180, 140);
var rgbCopy = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 450, 40, 180, 140);

foreach (var shape in new[] { source, themeCopy, rgbCopy })
{
    shape.FillFormat.FillType = FillType.Solid;
    shape.LineFormat.FillFormat.FillType = FillType.NoFill;
}

var sourceColor = source.FillFormat.SolidFillColor;
sourceColor.SchemeColor = SchemeColor.Accent1;
sourceColor.ColorTransform.Add(ColorTransformOperation.Tint, 0.75f);

themeCopy.FillFormat.SolidFillColor.CopyFrom(sourceColor);
rgbCopy.FillFormat.SolidFillColor.Color = source.FillFormat.GetEffective().SolidFillColor;

using var before = slide.GetImage(1f, 1f);
before.Save("color-copy-before.png", ImageFormat.Png);

// Theme references follow the new accent color; the fixed RGB value does not.
presentation.MasterTheme.ColorScheme.Accent1.Color = Color.OrangeRed;

using var after = slide.GetImage(1f, 1f);
after.Save("color-copy-after.png", ImageFormat.Png);
presentation.Save("copied_colors.pptx", SaveFormat.Pptx);

テーマ変更前は、3 つの塗りつぶしがすべて一致しています:

ソース、CopyFrom 宛先、固定 RGB 宛先の同一ブルー塗りつぶし

テーマ変更後、ソースとテーマ対応コピーは新しいアクセントカラーに従い、ティントを保持します。固定 RGB コピーは青のままです:

ソースと CopyFrom 宛先がオレンジ塗りつぶし、固定 RGB 宛先は青のまま

The copied definition is independent of the source. Subsequently changing the source’s theme reference, assigning another color, or adding or removing its transformations does not change the destination. A theme change still affects both shapes because each independently refers to the same theme color.

Saving and reopening the PPTX preserves the copied color’s IColorFormat.ColorType, theme reference, and IColorFormat.ColorTransform collection; it does not flatten the definition to RGB.

When copying between different theme contexts, such as presentations with different accent palettes, the copied reference resolves against the destination’s theme. The same reference and transformations can therefore produce a different visible color. See プレゼンテーションテーマ for more about configuring theme colors.

スライドの背景を形状の塗りつぶしとして使用

Use IAutoShape.UseBackgroundFill when a shape should match the slide background, for example in a presentation template. Set IFillFormat.FillType to FillType.NotDefined first to remove any explicit fill. With this combination, the shape uses the slide background instead of its style fill. Enabling the flag alone does not remove an existing explicit fill.

This differs from FillType.NoFill: background fill covers objects behind the shape with the corresponding area of the slide background, while no fill leaves those objects visible. A gradient remains aligned with the slide rather than restarting inside each shape.

The following example creates a master background inherited by the slide and places a magenta band behind three rectangles. The left rectangle uses the background, the middle rectangle has no fill, and the right rectangle switches from background fill to an explicit orange fill.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
presentation.SlideSize.SetSize(720, 400, SlideSizeScaleType.DoNotScale);
var slide = presentation.Slides[0];

// マスターからグラデーションを継承します。
slide.Background.Type = BackgroundType.NotDefined;
slide.LayoutSlide.Background.Type = BackgroundType.NotDefined;
var background = slide.LayoutSlide.MasterSlide.Background;
background.Type = BackgroundType.OwnBackground;
background.FillFormat.FillType = FillType.Gradient;
var gradient = background.FillFormat.GradientFormat;
gradient.GradientShape = GradientShape.Linear;
gradient.LinearGradientAngle = 0;
gradient.GradientStops.Clear();
gradient.GradientStops.Add(0, Color.DarkBlue);
gradient.GradientStops.Add(1, Color.LightSkyBlue);

// このオブジェクトを最初に追加して、矩形の背面に表示されるようにします。
var band = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 25, 150, 670, 100);
band.FillFormat.FillType = FillType.Solid;
band.FillFormat.SolidFillColor.Color = Color.Magenta;
band.LineFormat.FillFormat.FillType = FillType.NoFill;

var backgroundShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 100, 160, 200);
backgroundShape.FillFormat.FillType = FillType.NotDefined;
backgroundShape.UseBackgroundFill = true;

var noFillShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 280, 100, 160, 200);
noFillShape.FillFormat.FillType = FillType.NoFill;

var explicitShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 510, 100, 160, 200);
explicitShape.FillFormat.FillType = FillType.NotDefined;
explicitShape.UseBackgroundFill = true;

// 明示的な塗りつぶしを適用する前に背景塗りつぶしを無効にします。
explicitShape.UseBackgroundFill = false;
explicitShape.FillFormat.FillType = FillType.Solid;
explicitShape.FillFormat.SolidFillColor.Color = Color.Orange;

presentation.Save("background_fill.pptx", SaveFormat.Pptx);

結果(左から右へ):background fill、no fill、explicit fill。 マゼンタ帯は左と右の矩形で隠れますが、中央の矩形を通しては可視のままです。

背景塗りつぶし、塗りつぶしなし、明示的オレンジ塗りつぶしとマゼンタ帯

The same behavior applies to a background defined directly on the slide or inherited from a layout. The shape uses the slide’s resolved background; the example leaves both the slide and layout backgrounds undefined so the master supplies it.

透明度の設定

For a solid fill, the color’s alpha component controls opacity: 0 is fully transparent and 255 is fully opaque. This example uses an alpha value of 128 for a yellow rectangle, making it approximately 50% transparent so the rectangle underneath remains visible. The color is assigned through IFillFormat.SolidFillColor.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

const int alpha = 128;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var solidShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);

var transparentShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 80, 80, 150, 75);
transparentShape.FillFormat.FillType = FillType.Solid;
transparentShape.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Yellow);

presentation.Save("shape_transparency.pptx", SaveFormat.Pptx);

透明な形状

形状の回転

Use IShape.Rotation to rotate a shape in the slide plane. The angle is measured in degrees; this example rotates a rectangle 5 degrees clockwise and saves the presentation.

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

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.Rotation = 5;

presentation.Save("shape_rotation.pptx", SaveFormat.Pptx);

形状の回転

3D ベベル効果の追加

Use IShape.ThreeDFormat to give a shape depth and beveled edges. This example creates a green ellipse with an orange outline, sets IThreeDFormat.Depth to 4 points, and applies a circular top bevel with a width and height of 6 points through IThreeDFormat.BevelTop. A front‑facing orthographic camera and three‑point lighting make the bevel visible.

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Ellipse, 50, 50, 100, 100);
shape.FillFormat.FillType = FillType.Solid;
shape.FillFormat.SolidFillColor.Color = Color.Green;
shape.LineFormat.FillFormat.FillType = FillType.Solid;
shape.LineFormat.FillFormat.SolidFillColor.Color = Color.Orange;
shape.LineFormat.Width = 2.0;
shape.ThreeDFormat.Depth = 4;
shape.ThreeDFormat.BevelTop.BevelType = BevelPresetType.Circle;
shape.ThreeDFormat.BevelTop.Height = 6;
shape.ThreeDFormat.BevelTop.Width = 6;
shape.ThreeDFormat.Camera.CameraType = CameraPresetType.OrthographicFront;
shape.ThreeDFormat.LightRig.LightType = LightRigPresetType.ThreePt;
shape.ThreeDFormat.LightRig.Direction = LightingDirection.Top;

presentation.Save("3D_bevel_effect.pptx", SaveFormat.Pptx);

3D ベベル効果

3D 回転効果の追加

A shape’s 3D view is controlled by its camera settings. This example uses ICamera.SetRotation with latitude, longitude, and revolution angles of 40, 35, and 20 degrees, respectively. It selects an isometric camera through ICamera.CameraType and balanced lighting through ILightRig.LightType, then saves the formatted rectangle.

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

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var autoShape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
autoShape.TextFrame.Text = "Hello, Aspose!";
autoShape.ThreeDFormat.Camera.CameraType = CameraPresetType.IsometricLeftUp;
autoShape.ThreeDFormat.Camera.SetRotation(40, 35, 20);
autoShape.ThreeDFormat.LightRig.LightType = LightRigPresetType.Balanced;

presentation.Save("3D_rotation_effect.pptx", SaveFormat.Pptx);

3D 回転効果

形状の白黒レンダリングの制御

The IShape.BlackWhiteMode property specifies how an individual shape is rendered when a presentation is viewed or processed in black‑and‑white mode. It does not enable black‑and‑white display by itself, and it does not change the shape’s fill, line, or other formatting in normal color mode.

Use a value from the BlackWhiteMode enumeration to select the desired behavior. For example, Automatic lets the rendering application choose the conversion, Gray and LightGray use gray coloring, BlackWhite uses only black and white, Black and White force a single color, Color preserves normal coloring, and Hidden omits the shape in black‑and‑white mode. NotDefined means that no shape‑level mode is assigned.

This example creates an orange rectangle and sets IShape.BlackWhiteMode to gray. The saved shape retains its orange fill for normal color display and requests gray coloring from applications that honor black‑and‑white display settings:

using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);
shape.FillFormat.FillType = FillType.Solid;
shape.FillFormat.SolidFillColor.Color = Color.Orange;
shape.BlackWhiteMode = BlackWhiteMode.Gray;

presentation.Save("shape_black_white_mode.pptx", SaveFormat.Pptx);

In normal color mode, the rectangle retains its orange fill. In a black‑and‑white display workflow, it uses gray coloring because its mode is set to Gray. This lets you preserve a full‑color slide while defining a distinct appearance for printing, previewing, or other workflows that honor the presentation’s black‑and‑white display settings.

書式設定のリセット

Use ISlide.Reset to restore the position, size, and formatting of slide placeholders from their corresponding layout placeholders. This example requires an existing presentation with layout‑linked placeholders and resets every slide before saving. Shapes without corresponding layout placeholders are not reset.

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

using var presentation = new Presentation("sample.pptx");
foreach (var slide in presentation.Slides)
{
    slide.Reset();
}

presentation.Save("reset_formatting.pptx", SaveFormat.Pptx);

FAQ

形状の書式設定は最終的なプレゼンテーション ファイル サイズに影響しますか?

色、線スタイル、効果の設定は、埋め込み画像やメディアに比べて通常はごくわずかなデータしか追加しません。画像塗りつぶしは画像がプレゼンテーションに保存されるため、ファイル サイズが増加する可能性があります。

同一の書式設定を持つスライド上の図形を検出してグループ化するにはどうすればよいですか?

各図形の主要な書式プロパティ(塗りつぶし、線、効果設定)を比較します。すべての対応する値が一致すれば、スタイルが同一とみなして論理的にグループ化できます。これにより、後でのスタイル管理が簡素化されます。

カスタム図形スタイルのセットを別ファイルに保存して他のプレゼンテーションで再利用できますか?

はい。目的のスタイルを持つサンプル図形をテンプレート スライド デックまたは .POTX テンプレート ファイルに保存します。新しいプレゼンテーションを作成する際にテンプレートを開き、必要なスタイルの図形をクローンして、必要な場所に書式設定を再適用します。