How to extract the cropped image from ASP.NET Core Presentation in C#?
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 extract the cropped image from Presentation using C#.
Steps to extract the cropped image from Presentation using C#
Step 1: Create a new .NET Core console application project.
Step 2: Install the Syncfusion.PresentationRenderer.Net.Core NuGet package as a reference to your project from NuGet.org.
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;
using Syncfusion.Office;
using SkiaSharp;
Step 4: Include the below code snippet in Program.cs to extract the cropped image from Presentation.
//Open an existing PowerPoint Presentation.
using (FileStream fileStream = File.OpenRead("Sample.pptx"))
{
using (IPresentation presentation = Presentation.Open(fileStream))
{
//Get the first slide.
ISlide slide = presentation.Slides[0];
//Get the picture.
IPicture picture = slide.Pictures[0];
//Get cropped image stream.
using (MemoryStream stream = GetCroppedPictureData(new MemoryStream(picture.ImageData),picture.Crop))
{
//Save the image as stream.
using (FileStream fileStreamOutput = File.Create("Output.png"))
{
stream.CopyTo(fileStreamOutput);
}
}
}
}
Step 5: The following code example illustrates how to get the cropped picture data stream.
// Get the cropped picture data stream.
private static MemoryStream GetCroppedPictureData(MemoryStream imageStream, ICrop crop)
{
//Get the crop properties.
float imageWidth = crop.ContainerWidth;
float imageHeight = crop.ContainerHeight;
float offsetX = crop.OffsetX;
float offsetY = crop.OffsetY;
float rectWidth = crop.Width;
float rectHeight = crop.Height;
float rectX = (imageWidth - rectWidth) / 2;
float rectY = (imageHeight - rectHeight) / 2;
//Creating memory stream to return the cropped image stream.
MemoryStream outputStream = new MemoryStream();
//Create SKBitmap with respect to given image size.
using (SKBitmap m_sKBitmap = new SKBitmap(PointToPixel(imageWidth), PointToPixel(imageHeight), SKImageInfo.PlatformColorType, SKAlphaType.Premul))
{
//Create SKSurface from the sk bitmap.
using (SKSurface m_surface = SKSurface.Create(m_sKBitmap.Info))
{
//Draw the given image with in the image canvas.
using (SKCanvas m_canvas = m_surface.Canvas)
{
//Set the background color of the image as transparent.
m_canvas.Clear(new SKColor(0, 0, 0, 0));
//Create SKBitmap for the given image data.
using (SKBitmap inputSkBitmap = SKBitmap.Decode(SKData.Create(imageStream)))
{
// Translate the graphics and apply the Crop.OffsetX and Crop.OffsetY values.
m_canvas.Translate(PointToPixel(offsetX), PointToPixel(offsetY));
// Draw the cropped image at the specified picture position.
SKRect srcRect = new SKRect(PointToPixel(rectX), PointToPixel(rectY), PointToPixel(rectX + rectWidth), PointToPixel(rectY + rectHeight));
// Apply the crop values.
m_canvas.DrawBitmap(inputSkBitmap, srcRect, new SKPaint() { FilterQuality = SKFilterQuality.High });
//Save the SKSurface as stream.
m_surface.Snapshot().Encode(SKEncodedImageFormat.Png, 100).SaveTo(outputStream);
outputStream.Position = 0;
}
}
}
}
return outputStream;
}
/// <summary>
/// Convert a value from point to pixel.
/// </summary>
/// <param name="value">The value in point which needs to be converted to pixel.</param>
/// <returns>The value converted to pixel.</returns>
internal static int PointToPixel(double value)
{
return Convert.ToInt32((value * 96) / 72);
}
A complete working sample to extract the cropped image from Presentation using C# can be downloaded from GitHub.
By executing the program, you will get the image as follows.
Conclusion
I hope you enjoyed learning about how to extract the cropped image from 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!