Articles in this section
Category / Section

How to extract the cropped image from WinForms Presentation in C#?

4 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 extract the cropped image from Presentation using C#.

Steps to extract the cropped image from Presentation using C#

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

.NET Framework console application project

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

Install Syncfusion.Presentation.WinForms 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;
using Syncfusion.Office;

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 memoryStream = new MemoryStream();

   //Create the image with respect to given image size.
   using (Image outputImage = new Bitmap(PointToPixel(imageWidth), PointToPixel(imageHeight), PixelFormat.Format32bppPArgb))
   {
       //Draw the given image with in the image graphics.
       using (Graphics graphics = Graphics.FromImage(outputImage))
       {
           //Set the resolution for the image graphics
           (outputImage as Bitmap).SetResolution(graphics.DpiX, graphics.DpiY);
           //Set the background color of the image as transparent.
           graphics.Clear(Color.Transparent);
           //Set the graphics page unit is Point.
           graphics.PageUnit = GraphicsUnit.Point;

           //Create image for the given image data.
           using (Image inputImage = new Bitmap(imageStream))
           {
               // Translate the graphics and apply the Crop.OffsetX and Crop.OffsetY values.
               graphics.TranslateTransform(offsetX, offsetY);

               // Draw the cropped image at the specified picture position.
               RectangleF srcRect = new RectangleF(rectX, rectY, rectWidth, rectHeight);
               graphics.DrawImage(inputImage, srcRect);
           }
       }
       //Save the image as stream.
       outputImage.Save(memoryStream, ImageFormat.Png);
       memoryStream.Position = 0;
   }
   return memoryStream;
}

/// <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.

Output of extract cropped image in Presentation

Conclusion

I hope you enjoyed learning about how to extract the cropped image from WinForms Presentation in C#.

You can refer to our WinForms 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 WinForms 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