This article guides on how to replace picture in PowerPoint with C# REST API. You will learn how to replace picture in PowerPoint with C# REST Interface using the .NET-based Cloud SDK. It will provide complete details to upload the source files and download the modified files from the Cloud storage.
Prerequisite
- Create an account API credentials
- Download Aspose.Slides Cloud SDK for Dotnet to replace an image
- Setup C# project with the above SDK for replacing a picture
Steps to Replace Image in PPT with C# .NET-based API
- Initialize the SlidesApi class object to replace the picture
- Upload the target presentation with some images in it for replacement
- Load the source image into a Stream object to add it to the presentation
- Invoke the ReplaceImage() method by providing the presentation name, target image index, and new image stream
- Download the modified presentation with a new image
The steps describe how to replace picture in PowerPoint with C# REST Interface. Upload the source presentation to the Cloud storage, read the desired image into the Stream object, and call the ReplaceImage() method with the required arguments. Subsequently, call the DownloadFile method to fetch the updated presentation.
Code to Replace Picture in PPT with C# REST API
using Aspose.Slides.Cloud.Sdk; | |
using System; | |
using System.IO; | |
namespace SlideEditor | |
{ | |
class UpdatePresentation | |
{ | |
static void Main(string[] args) | |
{ | |
// Set up the API client with access credentials | |
var slidesApiClient = new SlidesApi("Client ID", "Secret"); | |
// Specify the name of the presentation file | |
string inputPresentation = "InputSlides.pptx"; | |
// Upload the presentation to the server for processing | |
var uploadResult = slidesApiClient.UploadFile(inputPresentation, new MemoryStream(File.ReadAllBytes(inputPresentation))); | |
// Open the image file to replace an image in the presentation | |
Stream replacementImage = File.OpenRead("ReplacementImage.png"); | |
// Replace the image on the first slide | |
slidesApiClient.ReplaceImage("InputSlides.pptx", 1, replacementImage); | |
Console.WriteLine("Image on index 1 has been replaced successfully."); | |
// Download the updated presentation back from the server | |
Stream modifiedPresentationStream = slidesApiClient.DownloadFile(inputPresentation); | |
// Save the updated presentation locally with a new name | |
using (var saveFileStream = new FileStream("UpdatedSlides.pptx", FileMode.Create, FileAccess.Write)) | |
{ | |
modifiedPresentationStream.CopyTo(saveFileStream); | |
} | |
Console.WriteLine("Updated presentation saved as 'UpdatedSlides.pptx'."); | |
} | |
} | |
} |
This code has demonstrated how to change picture shape in PowerPoint with C# RESTful Service. If you do not upload the presentation file to the Cloud storage, it will use any available presentation with the same name from the Cloud storage. You may get different results if you use the existing file name from the Cloud storage uploaded earlier in some other session.
This article has taught us the process of replacing an image in a presentation. To add a new picture, refer to the article on Add Picture to PowerPoint with C# REST API.