使用 C# 在 PowerPoint 中應用 3D 效果

PowerPoint 演示文稿中的 3D 效果使內容更具吸引力。使用 3D 文本或形狀,可以增強交互性並吸引觀眾的注意力。在 .NET 應用程序中處理 PowerPoint 的自動化時,您可能需要向演示文稿添加 3D 效果。為此,本文介紹瞭如何在 C# 中的 PowerPoint PPT 中應用 3D 效果。

用於在 PowerPoint 中應用 3D 效果的 .NET API

Aspose.Slides for .NET 是一個了不起的 API,它提供了一系列功能來實現 PowerPoint 自動化。使用 API,您可以無縫地創建和操作演示文稿。我們將使用此 API 在 PowerPoint 演示文稿中應用 3D 效果。您可以 下載 API 的 DLL 或使用 NuGet 安裝它。

PM> Install-Package Aspose.Slides.NET 

在 C# 中的 PowerPoint 中創建 3D 文本

以下是使用 C# 在 PowerPoint PPT 中創建 3D 文本片段的步驟。

下面的代碼示例顯示瞭如何使用 C# 在 PowerPoint 中創建 3D 文本。

// 創建演示文稿
using (Presentation presentation = new Presentation())
{
    // 創建一個矩形形狀
    IAutoShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 250, 250);
    shape.FillFormat.FillType = FillType.NoFill;
    shape.LineFormat.FillFormat.FillType = FillType.NoFill;
    shape.TextFrame.Text = "3D Text";

    // 獲取文本部分
    Portion portion = (Portion)shape.TextFrame.Paragraphs[0].Portions[0];
    portion.PortionFormat.FillFormat.FillType = FillType.Pattern;
    portion.PortionFormat.FillFormat.PatternFormat.ForeColor.Color = Color.DarkOrange;
    portion.PortionFormat.FillFormat.PatternFormat.BackColor.Color = Color.White;
    portion.PortionFormat.FillFormat.PatternFormat.PatternStyle = PatternStyle.LargeGrid;
    shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 128;

    // 訪問文本框
    ITextFrame textFrame = shape.TextFrame;

    // 設置“Arch Up”藝術字變換效果
    textFrame.TextFrameFormat.Transform = TextShapeType.ArchUp;

    // 應用 3D 效果
    textFrame.TextFrameFormat.ThreeDFormat.ExtrusionHeight = 3.5f;
    textFrame.TextFrameFormat.ThreeDFormat.Depth = 3;
    textFrame.TextFrameFormat.ThreeDFormat.Material = MaterialPresetType.Plastic;
    textFrame.TextFrameFormat.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
    textFrame.TextFrameFormat.ThreeDFormat.LightRig.LightType = LightRigPresetType.Balanced;
    textFrame.TextFrameFormat.ThreeDFormat.LightRig.SetRotation(0, 0, 40);
    textFrame.TextFrameFormat.ThreeDFormat.Camera.CameraType = CameraPresetType.PerspectiveContrastingRightFacing;

    // 保存演示文稿
    presentation.Save("3D-Text.pptx", SaveFormat.Pptx);
}

以下屏幕截圖顯示了上述代碼示例的輸出。

在 C# 中的 PowerPoint 中創建 3D 文本

在 C# 中的 PowerPoint 中創建 3D 形狀

與文本類似,您可以將 3D 效果應用於 PowerPoint 演示文稿中的形狀。以下是使用 C# 在 PowerPoint 中創建 3D 形狀的步驟。

以下代碼示例演示如何使用 C# 將 3D 效果應用於 PowerPoint 中的形狀。

// 創建演示文稿
using (Presentation presentation = new Presentation())
{
    // 添加新形狀
    IAutoShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // 設置文字
    shape.TextFrame.Text = "3D";
    shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 64;

    // 應用 3D 效果
    shape.ThreeDFormat.Camera.CameraType = CameraPresetType.OrthographicFront;
    shape.ThreeDFormat.Camera.SetRotation(20, 30, 40);
    shape.ThreeDFormat.LightRig.LightType = LightRigPresetType.Flat;
    shape.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
    shape.ThreeDFormat.Material = MaterialPresetType.Flat;
    shape.ThreeDFormat.ExtrusionHeight = 100;
    shape.ThreeDFormat.ExtrusionColor.Color = Color.Blue;

    // 保存演示文稿
    presentation.Save("3D-Shape.pptx", SaveFormat.Pptx);
}

以下是我們執行這段代碼後得到的 3D 形狀。

在 C# 中的 PowerPoint 中創建 3D 形狀

為 3D 形狀創建漸變

您還可以按照以下步驟對形狀應用漸變效果。

以下代碼示例演示如何將漸變效果應用於 PowerPoint 中的形狀。

// 創建演示文稿
using (Presentation presentation = new Presentation())
{
    // 添加新形狀
    IAutoShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // 設置文字
    shape.TextFrame.Text = "3D";
    shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 64;

    // 應用漸變
    shape.FillFormat.FillType = FillType.Gradient;
    shape.FillFormat.GradientFormat.GradientStops.Add(0, Color.Blue);
    shape.FillFormat.GradientFormat.GradientStops.Add(100, Color.LightBlue);

    // 應用 3D 效果
    shape.ThreeDFormat.Camera.CameraType = CameraPresetType.OrthographicFront;
    shape.ThreeDFormat.Camera.SetRotation(20, 30, 40);
    shape.ThreeDFormat.LightRig.LightType = LightRigPresetType.Flat;
    shape.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
    shape.ThreeDFormat.Material = MaterialPresetType.Flat;
    shape.ThreeDFormat.ExtrusionHeight = 100;
    shape.ThreeDFormat.ExtrusionColor.Color = Color.Blue;

    // 保存演示文稿
    presentation.Save("3D-Shape-Gradient.pptx", SaveFormat.Pptx);
}

以下是應用漸變效果後的 3D 形狀。

在 PowerPoint 中為 3D 形狀創建漸變

在 C# 中將 3D 效果應用於 PowerPoint 中的圖像

Aspose.Slides for .NET 還允許您對圖像應用 3D 效果。以下是在 C# 中執行此操作的步驟。

以下是使用 C# 在 PPT 中對圖像應用 3D 效果的步驟。

// 創建演示文稿
using (Presentation presentation = new Presentation())
{
    // 添加新形狀
    IAutoShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // 添加圖片
    shape.FillFormat.FillType = FillType.Picture;
    shape.FillFormat.PictureFillFormat.Picture.Image = presentation.Images.AddImage(File.ReadAllBytes("tiger.bmp"));
    shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;

    // 應用 3D 效果
    shape.ThreeDFormat.Camera.CameraType = CameraPresetType.OrthographicFront;
    shape.ThreeDFormat.Camera.SetRotation(20, 30, 40);
    shape.ThreeDFormat.LightRig.LightType = LightRigPresetType.Flat;
    shape.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
    shape.ThreeDFormat.Material = MaterialPresetType.Flat;
    shape.ThreeDFormat.ExtrusionHeight = 100;
    shape.ThreeDFormat.ExtrusionColor.Color = Color.DarkGray;

    // 保存演示文稿
    presentation.Save("3D-Image.pptx", SaveFormat.Pptx);
}

以下是我們應用 3D 效果後得到的結果圖像。

在 C# 中將 3D 效果應用於 PowerPoint 中的圖像

獲得免費許可證

您可以獲得 免費臨時許可證 以在沒有評估限制的情況下使用 Aspose.Slides for .NET。

結論

在本文中,您了解瞭如何使用 C# 在 PowerPoint 演示文稿中應用 3D 效果。我們已經介紹瞭如何創建 3D 文本或形狀以及如何將 3D 效果應用於 PPT 或 PPTX 演示文稿中的圖像。如果您想探索更多關於 Aspose.Slides for .NET 的信息,您可以訪問 文檔。此外,您可以將您的查詢發佈到我們的論壇

也可以看看