C#/VB.NET - Convert Images to a PowerPoint File
Creating a PowerPoint presentation from a set of images is one requirement that you may have at work. As an example, your company’s designer created a series of beautiful product flyers, and you may need to convert these pictures into PowerPoint in order to display them more effectively. By doing so, you do not need to create a PowerPoint document from scratch and save a lot of time. In this article, I am going to introduce how to convert images (JPG, JPEG, PNG, etc.) to PowerPoint presentations in C# and VB.NET using Spire.Presentation for .NET library.
Install Spire.Presentation for .NET
Method #1: Download Spire.Presentation and unzip the package somewhere on your disk to find the “BIN” folder. Spire.Presentation has the DLLs compiled for multiple versions of .NET Framework as well as for .NET Core and other platforms. Choose the DLLs from the folder that you exactly need and add them all as dependencies in your project.
Method #2: Create a .NET application in you Visual Studio, and install Spire.Presentation directly through NuGet. NuGet package manager will automatically install the correct version that fits in with your application.
Convert Images to a PowerPoint File in C# and VB.NET
The following are the steps to convert a group of images to a PowerPoint document using Spire.Presentation for .NET.
- Create a Presentation object.
- Set the slide size type to Screen 16x9.
- Get the image paths from a folder and store in a string array.
- Traverse all items in the array.
- Add a slide to the presentation using Presnetation.Slides.Add() method.
- Get a specific image from the string array and append it the image collection of the presentation using Presentation.Images.Append() method.
- Set the image as the background image of the slide.
- Save the presentation to a PowerPoint file using Presentation.SaveToFile() method.
[C#]
using System.Drawing;
using System.IO;
using Spire.Presentation;
using Spire.Presentation.Drawing;namespace ConvertImagesToPptx
{
class Program
{
static void Main(string[] args)
{
//Create a Presentation object
Presentation presentation = new Presentation(); //Set slide size type
presentation.SlideSize.Type = SlideSizeType.Screen16x9; //Remove the default slide
presentation.Slides.RemoveAt(0); //Get image file paths and store in a string array
string[] picFiles = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Images"); //Loop through the images
for (int i = 0; i < picFiles.Length; i++)
{
//Add a slide
ISlide slide = presentation.Slides.Append(); //Get a specific image
string imageFile = picFiles[i];
Image image = Image.FromFile(imageFile); //Append it to the image collection
IImageData imageData = presentation.Images.Append(image); //Set the image as the background image of the slide
slide.SlideBackground.Type = BackgroundType.Custom;
slide.SlideBackground.Fill.FillType = FillFormatType.Picture;
slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = imageData;
} //Save to file
presentation.SaveToFile("ImagesToPowerPoint.pptx",FileFormat.Pptx2013);
}
}
}
[VB.NET]
Imports System.Drawing
Imports System.IO
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Namespace ConvertImagesToPptx
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Presentation object
Dim presentation As Presentation = New Presentation()
'Set slide size type
presentation.SlideSize.Type = SlideSizeType.Screen16x9
'Revemo the default slide
presentation.Slides.RemoveAt(0)
'Get file paths in a string array
Dim picFiles() As String = Directory.GetFiles("C:\Users\Administrator\Desktop\Images")
'Loop through the images
Dim i As Integer
For i = 0 To picFiles.Length- 1 Step i + 1
'Add a slide
Dim slide As ISlide = presentation.Slides.Append()
'Get a specific image
Dim imageFile As String = picFiles(i)
Dim image As Image = Image.FromFile(imageFile)
'Append it to the image collection
Dim imageData As IImageData = presentation.Images.Append(image)
'Set the image as the background image of the slide
slide.SlideBackground.Type = BackgroundType.Custom
slide.SlideBackground.Fill.FillType = FillFormatType.Picture
slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch
slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = imageData
Next
'Save to file
presentation.SaveToFile("ImagesToPowerPoint.pptx",FileFormat.Pptx2013)
System.Diagnostics.Process.Start("ImagesToPowerPoint.pptx")
End Sub
End Class
End Namespace
Output