Áp dụng hiệu ứng 3D trong PowerPoint bằng C#

Hiệu ứng 3D trong bài thuyết trình PowerPoint giúp nội dung hấp dẫn hơn. Sử dụng văn bản hoặc hình dạng 3D, bạn có thể nâng cao tính tương tác và thu hút sự chú ý của khán giả. Trong khi làm việc trên việc tự động hóa PowerPoint từ bên trong các ứng dụng .NET, bạn có thể cần thêm hiệu ứng 3D vào bản trình bày. Để thực hiện điều đó, bài viết này trình bày cách áp dụng hiệu ứng 3D trong PowerPoint PPT trong C#.

.NET API để áp dụng hiệu ứng 3D trong PowerPoint

Aspose.Slides for .NET là một API tuyệt vời cung cấp một loạt các tính năng để triển khai tự động hóa PowerPoint. Sử dụng API, bạn có thể tạo và thao tác các bản trình bày một cách liền mạch. Chúng tôi sẽ sử dụng API này để áp dụng hiệu ứng 3D trong các bản trình bày PowerPoint. Bạn có thể [tải xuống] DLL của 2 API hoặc cài đặt nó bằng NuGet.

PM> Install-Package Aspose.Slides.NET 

Tạo văn bản 3D trong PowerPoint trong C#

Sau đây là các bước để tạo một đoạn văn bản 3D trong PowerPoint PPT bằng C#.

  • Đầu tiên, tạo một PPT mới hoặc tải một PPT hiện có bằng cách sử dụng lớp Trình bày.
  • Sau đó, thêm một hình chữ nhật mới bằng phương thức AddAutoShape().
  • Đặt các thuộc tính của hình dạng như kiểu tô, văn bản, v.v.
  • Lấy tham chiếu của văn bản bên trong hình dạng thành một đối tượng Portion.
  • Áp dụng định dạng cho phần văn bản.
  • Tham khảo hình dạng bên trong TextFrame.
  • Áp dụng hiệu ứng 3D bằng cách sử dụng các thuộc tính trong TextFrame.TextFrameFormat.ThreeDFormat.
  • Cuối cùng, lưu bản trình bày bằng phương thức Presentation.Save (String, SaveFormat).

Mẫu mã sau đây cho thấy cách tạo văn bản 3D trong PowerPoint trong C#.

// Tạo bản trình bày
using (Presentation presentation = new Presentation())
{
    // Tạo hình chữ nhật
    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";

    // Nhận phần văn bản
    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;

    // Truy cập khung văn bản
    ITextFrame textFrame = shape.TextFrame;

    // Thiết lập hiệu ứng chuyển đổi WordArt "Arch Up"
    textFrame.TextFrameFormat.Transform = TextShapeType.ArchUp;

    // Áp dụng hiệu ứng 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;

    // Lưu bản trình bày
    presentation.Save("3D-Text.pptx", SaveFormat.Pptx);
}

Ảnh chụp màn hình sau đây cho thấy đầu ra của mẫu mã ở trên.

Tạo văn bản 3D trong PowerPoint trong C#

Tạo Hình dạng 3D trong PowerPoint trong C#

Tương tự như văn bản, bạn có thể áp dụng hiệu ứng 3D cho các hình dạng trong bản trình bày PowerPoint. Sau đây là các bước để tạo hình dạng 3D trong PowerPoint trong C#.

Mẫu mã sau đây cho thấy cách áp dụng hiệu ứng 3D cho hình dạng trong PowerPoint bằng C#.

// Tạo bản trình bày
using (Presentation presentation = new Presentation())
{
    // Thêm một hình dạng mới
    IAutoShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // Đặt văn bản
    shape.TextFrame.Text = "3D";
    shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 64;

    // Áp dụng hiệu ứng 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;

    // Lưu bản trình bày
    presentation.Save("3D-Shape.pptx", SaveFormat.Pptx);
}

Sau đây là hình dạng 3D mà chúng ta nhận được sau khi thực hiện đoạn mã này.

Tạo Hình dạng 3D trong PowerPoint trong C#

Tạo Gradient cho Hình dạng 3D

Bạn cũng có thể áp dụng hiệu ứng gradient cho các hình dạng theo các bước bên dưới.

Mẫu mã sau đây cho thấy cách áp dụng hiệu ứng gradient cho hình dạng trong PowerPoint.

// Tạo bản trình bày
using (Presentation presentation = new Presentation())
{
    // Thêm một hình dạng mới
    IAutoShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // Đặt văn bản
    shape.TextFrame.Text = "3D";
    shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 64;

    // Áp dụng gradient
    shape.FillFormat.FillType = FillType.Gradient;
    shape.FillFormat.GradientFormat.GradientStops.Add(0, Color.Blue);
    shape.FillFormat.GradientFormat.GradientStops.Add(100, Color.LightBlue);

    // Áp dụng hiệu ứng 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;

    // Lưu bản trình bày
    presentation.Save("3D-Shape-Gradient.pptx", SaveFormat.Pptx);
}

Sau đây là hình dạng 3D sau khi áp dụng hiệu ứng gradient.

Tạo Gradient cho Hình dạng 3D trong PowerPoint

Áp dụng Hiệu ứng 3D cho Hình ảnh trong PowerPoint trong C#

Aspose.Slides for .NET cũng cho phép bạn áp dụng các hiệu ứng 3D cho hình ảnh. Sau đây là các bước để thực hiện thao tác này trong C#.

Sau đây là các bước để áp dụng hiệu ứng 3D cho hình ảnh trong PPT bằng C#.

// Tạo bản trình bày
using (Presentation presentation = new Presentation())
{
    // Thêm một hình dạng mới
    IAutoShape shape = presentation.Slides[0].Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);

    // Thêm hình ảnh
    shape.FillFormat.FillType = FillType.Picture;
    shape.FillFormat.PictureFillFormat.Picture.Image = presentation.Images.AddImage(File.ReadAllBytes("tiger.bmp"));
    shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;

    // Áp dụng hiệu ứng 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;

    // Lưu bản trình bày
    presentation.Save("3D-Image.pptx", SaveFormat.Pptx);
}

Sau đây là hình ảnh kết quả mà chúng tôi nhận được sau khi áp dụng hiệu ứng 3D.

Áp dụng Hiệu ứng 3D cho Hình ảnh trong PowerPoint trong C#

Nhận giấy phép miễn phí

Bạn có thể nhận giấy phép tạm thời miễn phí để sử dụng Aspose.Slides for .NET mà không có giới hạn đánh giá.

Sự kết luận

Trong bài này, bạn đã học cách áp dụng hiệu ứng 3D trong bản trình bày PowerPoint bằng C#. Chúng tôi đã trình bày cách tạo văn bản hoặc hình dạng 3D và áp dụng hiệu ứng 3D cho hình ảnh trong bản trình bày PPT hoặc PPTX. Trong trường hợp bạn muốn khám phá thêm về Aspose.Slides for .NET, bạn có thể truy cập tài liệu. Ngoài ra, bạn có thể đăng các truy vấn của mình lên diễn đàn của chúng tôi.

Xem thêm