Bài viết này hướng dẫn cách thêm Hình mờ trong PowerPoint bằng API C# REST. Bạn sẽ tìm hiểu cách tạo hình mờ cho ảnh trong PowerPoint bằng Giao diện C# REST bằng cách sử dụng SDK dựa trên .NET. Nó sẽ chia sẻ các chi tiết để tùy chỉnh hình ảnh trước khi thêm nó làm hình mờ vào bài thuyết trình.
Điều kiện tiên quyết
Tải xuống Aspose.Slides Cloud SDK for Dotnet to add an image watermark
Thiết lập dự án C# với SDK ở trên để chèn hình mờ
Các bước để chèn hình mờ trong PowerPoint bằng API C# REST
- Khởi tạo Aspose SlidesApi bằng thông tin đăng nhập của khách hàng
- Tải file trình bày lên máy chủ bằng phương thức UploadFile()
- Đọc dữ liệu hình ảnh sẽ được sử dụng cho hình mờ thành một mảng byte
- Thiết lập khung ảnh sẽ giữ hình mờ bằng lớp PictureFrame
- Thêm hình ảnh dưới dạng hình mờ vào bản trình bày bằng phương pháp CreateImageWatermark()
- Tải xuống bản trình bày đã sửa đổi có hình mờ được thêm bằng phương thức DownloadFile()
- Lưu bản trình bày đã cập nhật cục bộ
Các bước này mô tả cách tạo hình mờ cho ảnh trong PowerPoint bằng C# RESTful Service. Khởi tạo đối tượng SlidesApi, tải bản trình bày lên máy chủ và đọc hình ảnh mờ thành một mảng byte. Thiết lập đối tượng PictureFrame để đặt tham số hình mờ và thêm nó vào bản trình bày bằng phương thức CreateImageWatermark().
Mã để thêm hình mờ hình ảnh trong PowerPoint bằng API dựa trên C# .NET
using Aspose.Slides.Cloud.Sdk; | |
using Aspose.Slides.Cloud.Sdk.Model; | |
using System; | |
using System.IO; | |
namespace PresentationProcessor | |
{ | |
// This class demonstrates how to modify a slide deck by adding an image watermark. | |
class ModifySlide | |
{ | |
static void Main(string[] args) | |
{ | |
// Initialize the Aspose Slides API with client credentials (replace with actual credentials) | |
var slideService = new SlidesApi("ID", "KEY"); | |
// Define the name of the presentation file to be modified | |
string inputFileName = "OriginalSlides.pptx"; | |
// Specify the local path of the image that will be used as a watermark | |
string imagePath = "NewImage.png"; | |
// Upload the presentation file to the server | |
var uploadResult = slideService.UploadFile(inputFileName, new MemoryStream(File.ReadAllBytes(inputFileName))); | |
// Read the image data that will be used for the watermark | |
byte[] imageContent = File.ReadAllBytes(imagePath); | |
// Set up the image frame that will hold the watermark image | |
PictureFrame newImageFrame = new PictureFrame | |
{ | |
X = 50, // Horizontal position of the watermark (from the left) | |
Y = 50, // Vertical position of the watermark (from the top) | |
Width = 800, // Width of the watermark image | |
Height = 450, // Height of the watermark image | |
PictureFillFormat = new PictureFill | |
{ | |
Base64Data = Convert.ToBase64String(imageContent), // The image data encoded in base64 | |
PictureFillMode = PictureFill.PictureFillModeEnum.Stretch, // Image will stretch to fit the frame | |
} | |
}; | |
// Add the image as a watermark to the presentation | |
slideService.CreateImageWatermark(inputFileName, null, newImageFrame); | |
// Download the modified presentation with the watermark added | |
Stream modifiedFileStream = slideService.DownloadFile(inputFileName); | |
// Save the updated presentation locally | |
using (var localFileStream = new FileStream("UpdatedSlideDeck.pptx", FileMode.Create, FileAccess.Write)) | |
{ | |
// Copy the content of the downloaded file stream to the local file stream | |
modifiedFileStream.CopyTo(localFileStream); | |
} | |
} | |
} | |
} |
Mã này trình bày cách tạo hình mờ trong PowerPoint bằng API dựa trên C# .NET. Định cấu hình khung ảnh để đặt vị trí hình mờ từ góc trên cùng bên trái, kích thước và định dạng tô của nó. Bạn cũng có thể đặt DPI, cắt ảnh, độ lệch và tỷ lệ ô xếp cũng như dữ liệu SVG.
Bài viết này đã dạy chúng tôi cách tạo hình mờ cho một hình ảnh trong PowerPoint bằng API mã C# thấp. Để xóa hình mờ khỏi bản trình bày, hãy tham khảo bài viết Xóa hình mờ khỏi bản trình bày bằng API C# REST.