How to extract an .EMF images from the PPTX file in ASP.NET Core?
Syncfusion® Presentation is a .NET PowerPoint Library used to create, read, edit, and convert PowerPoint files to PDF and images programmatically without Microsoft Office or interop dependencies. Using this library, you can extract an .EMF images from the PPTX file in .NET using C#.
Steps to extract an .EMF images from the PPTX file in .NET using C#:
- Create a new C# .NET Core console application.
- Install the Syncfusion.PresentationRenderer.Net.Core NuGet package as a reference to your .NET Core application from NuGet.org.
Note:
-
The Syncfusion.Compression library is a dependent package of the PresentationRenderer NuGet package. Once PresentationRenderer gets installed, the Compression library will automatically be installed as its dependent package.
-
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
- Include the following namespace in the Program.cs file:
C#
using Syncfusion.Presentation;
using Syncfusion.Drawing;
- Use the following code snippet for extract EMF images from PPTX file in .NET.
C#
// Open the PowerPoint file as a stream.
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.pptx"), FileMode.Open, FileAccess.Read))
{
// Load the presentation from the stream.
using (IPresentation presentation = Presentation.Open(inputStream))
{
// Extract EMF images from Masters and their Layout slides
foreach (IMasterSlide master in presentation.Masters)
{
// Process shapes placed on the master slide
foreach (IShape shape in master.Shapes)
{
ProcessShape(shape);
}
// Process shapes placed on each layout slide under this master
foreach (ILayoutSlide layoutSlide in master.LayoutSlides)
{
foreach (IShape shape in layoutSlide.Shapes)
{
ProcessShape(shape);
}
}
}
// Extract EMF images from Normal slides
foreach (ISlide slide in presentation.Slides)
{
foreach (IShape shape in slide.Shapes)
{
ProcessShape(shape);
}
}
}
}
- The following code example shows the Processes a PowerPoint shape by extracting EMF images from picture shapes and picture‑fills. Recursively scans within grouped shapes to ensure all nested EMF images are detected.
public static int imageIndex = 0;
public static string outputPath = @"Output";
private static void ProcessShape(IShape shape)
{
// If the shape is a picture with EMF format
if (shape is IPicture picture && picture.ImageFormat == ImageFormat.Emf)
{
SaveEMFImage(picture);
}
// Shape is not a picture object, but its FILL contains a picture(Picture Fill)
if (shape.Fill != null && shape.Fill.FillType == FillType.Picture)
{
// Validate bytes exist and check if those bytes represent an EMF file
var bytes = shape.Fill.PictureFill.ImageBytes;
if (bytes != null && bytes.Length > 0 && IsEmf(bytes))
{
string filePath = Path.Combine(outputPath, $"Slide_Image_{++imageIndex}.emf");
File.WriteAllBytes(filePath, bytes);
}
}
// If the shape is a group, process child shapes
if (shape is IGroupShape group)
{
foreach (IShape child in group.Shapes)
{
ProcessShape(child);
}
}
}
private static void SaveEMFImage(IPicture picture)
{
byte[] imageData = picture.ImageData;
if (imageData != null && imageData.Length > 0)
{
string extension = picture.ImageFormat.ToString().ToLower();
string filePath = Path.Combine(outputPath, $"Slide_Image_{++imageIndex}.{extension}");
File.WriteAllBytes(filePath, imageData);
}
}
private static bool IsEmf(byte[] bytes)
{
if (bytes == null || bytes.Length < 44) return false;
// EMF signature " EMF" = 0x20 0x45 0x4D 0x46 (little endian uint32 => 0x464D4520)
// At offset 40..43
uint signature = BitConverter.ToUInt32(bytes, 40);
return signature == 0x464D4520;
}
You can download a complete working sample from GitHub.
Take a moment to peruse the documentation, where you can find basic presentation document processing options along with features like clone and merge slides and encrypt and decrypt PowerPoint presentations and most importantly PDF and image conversion with code examples.
Explore more about the rich set of Syncfusion® PowerPoint Framework features.
Conclusion
I hope you enjoyed learning how to extract an .EMF images from the PPTX file in .NET Core.
You can refer to our .NET PowerPoint Library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
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!