Articles in this section
Category / Section

How to crop an image in ASP.NET Core Presentation using C#?

2 mins read

Syncfusion PowerPoint is a .NET PowerPoint library used to create, read, and edit PowerPoint presentation programmatically without Microsoft PowerPoint or interop dependencies. Using this library, you can crop an image in a Presentation using C#.

Steps to crop an image in a Presentation using C#

Step 1: Create a new .NET Core console application project.

.NET Core console application project

Step 2: Install the Syncfusion.Presentation.Net.Core NuGet package as a reference to your project from NuGet.org.

Install Syncfusion.Presentation.Net.Core NuGet Package

Note: Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

Step 3: Include the following namespaces in Program.cs file.

using Syncfusion.Presentation;

Step 4: Include the below code snippet in Program.cs to crop an image in a Presentation.

//Open an existing PowerPoint Presentation.
using (FileStream inputStream = new FileStream("Sample.pptx", FileMode.Open, FileAccess.Read))
{
   using (IPresentation pptxDoc = Presentation.Open(inputStream))
   {
       //Retrieve the first slide from the Presentation.
       ISlide slide = pptxDoc.Slides[0];
       //Retrieve the first picture from the slide.
       IPicture picture = slide.Pictures[0];

      //Apply bounding box size and position.
      picture.Crop.ContainerWidth = 114.48f;
      picture.Crop.ContainerHeight = 56.88f;
      picture.Crop.ContainerLeft = 94.32f;
      picture.Crop.ContainerTop = 128.16f;
      
      //Apply cropping size and offsets.
      picture.Crop.Width = 900.72f;
      picture.Crop.Height = 74.88f;
      picture.Crop.OffsetX = 329.04f;
      picture.Crop.OffsetY = -9.36f;

       //Save the PowerPoint Presentation as stream.
       using (FileStream outputStream = new FileStream("Output.pptx", FileMode.Create))
       {
           pptxDoc.Save(outputStream);
       }        
   }       
}

Note: The bounding box properties (ContainerLeft, ContainerTop, ContainerRight, ContainerBottom) must be set before applying the cropping properties for proper functionality.

A complete working sample to crop an image in a Presentation using C# can be downloaded from GitHub.

By executing the program, you will get the PowerPoint presentation as follows.

Output of crop image in PowerPoint

Conclusion

I hope you enjoyed learning about how to crop an image in ASP.NET Core Presentation using C#.

You can refer to our ASP.NET Core Presentation feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core Presentation example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied