Articles in this section
Category / Section

How to crop and insert image in ASP.NET Core Presentation in 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 and insert image while creating Presentation using C#.

Steps to crop and insert image in 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 and insert image in PowerPoint slide.

//Create a new instance of PowerPoint Presentation file.
using (IPresentation pptxDoc = Presentation.Create())
{
   //Add a blank slide.
   ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
   //Get a picture as stream.
   using (FileStream pictureStream = new FileStream("Image.png", FileMode.Open))
   {
       //Add the picture to a slide by specifying its size and position.
       IPicture picture = slide.Pictures.AddPicture(pictureStream, 0, 0, 250, 250);

       //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 and insert image in 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 and insert image in ASP.NET Core Presentation in 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  to leave a comment
Access denied
Access denied