There are times when you need to create a PowerPoint document from a group of pre-created image files. As an example, you have been provided with some beautiful flyers by your company, and you need to combine them into a single PowerPoint document in order to display each picture in an orderly manner. In this article, you will learn how to convert image files (in any popular image format) to a PowerPoint document in C# and VB.NET using Spire.Presentation for .NET.
- Convert Image to Background in PowerPoint in C# and VB.NET
- Convert Image to Shape in PowerPoint in C# and VB.NET
- Convert Image to PowerPoint with Customized Slide Size in C# and VB.NET
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Presentation
Convert Image to Background in PowerPoint in C# and VB.NET
When images are converted as background of each slide in a PowerPoint document, they cannot be moved or scaled. The following are the steps to convert a set of images to a PowerPoint file as background images using Spire.Presentation for .NET.
- Create a Presentation object.
- Set the slide size type to Sreen16x9.
- Get the image paths from a folder and save in a string array.
- Traverse through the images.
- Get a specific image and append it to the image collection of the document using Presentation.Images.Append() method.
- Add a slide to the document using Presentation.Slides.Append() method.
- Set the image as the background of the slide through the properties under ISlide.SlideBackground object.
- Save the document to a PowerPoint file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; using System.IO; namespace ConvertImageToBackground { 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 file paths 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("ImagesToBackground.pptx", FileFormat.Pptx2013); } } }
Convert Image to Shape in PowerPoint in C# and VB.NET
If you would like the images are moveable and resizable in the PowerPoint file, you can convert them as shapes. Below are the steps to convert images to shapes in a PowerPoint document using Spire.Presentation for .NET.
- Create a Presentation object.
- Set the slide size type to Sreen16x9.
- Get the image paths from a folder and save in a string array.
- Traverse through the images.
- Get a specific image and append it to the image collection of the document using Presentation.Images.Append() method.
- Add a slide to the document using Presentation.Slides.Append() method.
- Add a shape with the size equal to the slide using ISlide.Shapes.AppendShape() method.
- Fill the shape with the image through the properties under IAutoShape.Fill object.
- Save the document to a PowerPoint file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; using System.IO; namespace ConvertImageToShape { 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 file paths 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); //Add a shape with a size equal to the slide IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(new PointF(0, 0), presentation.SlideSize.Size)); //Fill the shape with image shape.Line.FillType = FillFormatType.None; shape.Fill.FillType = FillFormatType.Picture; shape.Fill.PictureFill.FillType = PictureFillType.Stretch; shape.Fill.PictureFill.Picture.EmbedImage = imageData; } //Save to file presentation.SaveToFile("ImageToShape.pptx", FileFormat.Pptx2013); } } }
Convert Image to PowerPoint with Customized Slide Size in C# and VB.NET
If the aspect ratio of your images is not 16:9, or they are not in a standard slide size, you can create slides based on the actual size of the pictures. This will prevent the image from being over stretched or compressed. The following are the steps to convert images to a PowerPoint document with customized slide size using Spire.Presentation for .NET.
- Create a Presentation object.
- Create a PdfUnitConvertor object, which is used to convert pixel to point.
- Get the image paths from a folder and save in a string array.
- Traverse through the images.
- Get a specific image and append it to the image collection of the document using Presentation.Images.Append() method.
- Get the image width and height, and convert them to point.
- Set the slide size of the presentation based on the image size through Presentation.SlideSize.Size property.
- Add a slide to the document using Presentation.Slides.Append() method.
- Set the image as the background image of the slide through the properties under ISlide.SlideBackground object.
- Save the document to a PowerPoint file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf.Graphics; using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; using System.IO; namespace CustomSlideSize { class Program { static void Main(string[] args) { //Create a Presentation object Presentation presentation = new Presentation(); //Remove the default slide presentation.Slides.RemoveAt(0); //Get file paths in a string array string[] picFiles = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Images"); //Create a PdfUnitConvertor object PdfUnitConvertor convertor = new PdfUnitConvertor(); //Loop through the images for (int i = 0; i < picFiles.Length; i++) { //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); //Get image height and width in pixel int height = imageData.Height; int width = imageData.Width; //Convert pixel to point float widthPoint = convertor.ConvertUnits(width, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point); float heightPoint= convertor.ConvertUnits(height, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point); //Set slide size presentation.SlideSize.Size = new SizeF(widthPoint, heightPoint); //Add a slide ISlide slide = presentation.Slides.Append(); //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("CustomizeSlideSize.pptx", FileFormat.Pptx2013); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.