Convert PowerPoint to Image in ASP.NET MVC
26 Feb 20258 minutes to read
Syncfusion® PowerPoint is a .NET PowerPoint library used to create, read, edit and convert PowerPoint presentation programmatically without Microsoft PowerPoint or interop dependencies. Using this library, you can convert a PowerPoint to image in ASP.NET MVC.
Steps to convert PowerPoint to Image programmatically
Prerequisites:
- Visual Studio 2022.
- Install .NET desktop development workload with necessary .NET Framework SDK.
Step 1: Create a new C# ASP.NET MVC application project.
Step 2: Select the MVC template to create the project.
Step 3: Install the Syncfusion.Presentation.AspNet.Mvc5 NuGet package as reference to your .NET Standard applications from NuGet.org.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.
Step 4: Include the following namespace in that HomeController.cs file.
using Syncfusion.Presentation;
Step 5: A default action method named Index will be present in HomeController.cs. Right click on this action method and select Go To View where you will be directed to its associated view page Index.cshtml.
Step 6: Add a new button in the Index.cshtml as shown below.
@{
Html.BeginForm("ConvertPPTXtoImage", "Home", FormMethod.Get);
{
<div>
<input type="submit" value="Convert PPTX to Image" style="width:200px;height:27px" />
</div>
}
Html.EndForm();
}
Step 7: Add the below code snippet in HomeController.cs to convert a PowerPoint to image in ASP.NET MVC.
public void ConvertPPTXtoImage()
{
//Open the file as Stream.
using (FileStream pathStream = new FileStream(Server.MapPath("~/App_Data/Input.pptx"), FileMode.Open, FileAccess.Read))
{
//Opens a PowerPoint Presentation.
using (IPresentation pptxDoc = Presentation.Open(pathStream))
{
//Convert the first slide into image.
Image image = pptxDoc.Slides[0].ConvertToImage(Syncfusion.Drawing.ImageType.Metafile);
//Saves the image file to MemoryStream.
MemoryStream stream = new MemoryStream();
//Download image file in the browser.
ExportAsImage(image, "PPTXToImage.Jpeg", ImageFormat.Jpeg, HttpContext.ApplicationInstance.Response);
}
}
}
//To download the image file.
protected void ExportAsImage(Image image, string fileName, ImageFormat imageFormat, HttpResponse response)
{
if (ControllerContext == null)
throw new ArgumentNullException("Context");
string disposition = "content-disposition";
response.AddHeader(disposition, "attachment; filename=" + fileName);
if (imageFormat != ImageFormat.Emf)
image.Save(response.OutputStream, imageFormat);
Response.End();
}
Step 8: Build the project.
Click on Build → Build Solution or press Ctrl+Shift+B to build the project.
Step 9: Run the project.
Click the Start button (green arrow) or press F5 to run the app.
You can download a complete working sample from GitHub.
By executing the program, you will get the image as follows.
Prerequisites:
- JetBrains Rider.
- Install .NET Framework Developer Pack.
Step 1. Open JetBrains Rider and create a new ASP.NET MVC web application project.
- Launch JetBrains Rider.
- Click New Solution on the welcome screen.
- In the New Solution dialog, select Project Type as Web.
- Enter a project name and specify the location.
- Select the target framework as Full Framework and choose the desired version.
- Select Template as Web App.
- Click create.
Step 2: Install the NuGet package from NuGet.org.
- Click the NuGet icon in the Rider toolbar and type Syncfusion.PresentationToPdfConverter.AspNet.Mvc5 in the search bar.
- Ensure that nuget.org is selected as the package source.
- Select the latest Syncfusion.PresentationToPdfConverter.AspNet.Mvc5 NuGet package from the list.
- Click the + (Add) button to add the package.
- Click the Install button to complete the installation.
NOTE
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your application to use our components.
Step 3: Include the following namespace in that HomeController.cs file.
using Syncfusion.Presentation;
Step 4: A default action method named Index will be present in HomeController.cs. Right click on this action method and select Go To View where you will be directed to its associated view page Index.cshtml.
Step 5: Add a new button in the Index.cshtml as shown below.
@{
Html.BeginForm("ConvertPPTXtoImage", "Home", FormMethod.Get);
{
<div>
<input type="submit" value="Convert PPTX to Image" style="width:200px;height:27px" />
</div>
}
Html.EndForm();
}
Step 6: Add the below code snippet in HomeController.cs to convert a PowerPoint to image in ASP.NET MVC.
public void ConvertPPTXtoImage()
{
//Open the file as Stream.
using (FileStream pathStream = new FileStream(Server.MapPath("~/App_Data/Input.pptx"), FileMode.Open, FileAccess.Read))
{
//Opens a PowerPoint Presentation.
using (IPresentation pptxDoc = Presentation.Open(pathStream))
{
//Convert the first slide into image.
Image image = pptxDoc.Slides[0].ConvertToImage(Syncfusion.Drawing.ImageType.Metafile);
//Saves the image file to MemoryStream.
MemoryStream stream = new MemoryStream();
//Download image file in the browser.
ExportAsImage(image, "PPTXToImage.Jpeg", ImageFormat.Jpeg, HttpContext.ApplicationInstance.Response);
}
}
}
//To download the image file.
protected void ExportAsImage(Image image, string fileName, ImageFormat imageFormat, HttpResponse response)
{
if (ControllerContext == null)
throw new ArgumentNullException("Context");
string disposition = "content-disposition";
response.AddHeader(disposition, "attachment; filename=" + fileName);
if (imageFormat != ImageFormat.Emf)
image.Save(response.OutputStream, imageFormat);
Response.End();
}
Step 7: Build the project.
Click the Build button in the toolbar or press Ctrl+Shift+B to build the project.
Step 8: Run the project.
Click the Run button (green arrow) in the toolbar or press F5 to run the app.
You can download a complete working sample from GitHub.
By executing the program, you will get the image as follows.
Click here to explore the rich set of Syncfusion® PowerPoint Library (Presentation) features.
An online sample link to convert PowerPoint Presentation to image in ASP.NET MVC.