Incorporating images into a PowerPoint presentation is crucial for captivating your audience and reinforcing your message. Visual elements not only break up text but also make complex information more digestible. From personal photos to professional graphics, the right images can elevate your slides and enhance understanding.
In this article, you will learn how to insert images from your local disk or a URL into a PowerPoint slide using C# and Spire.Presentation for .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
Insert an Image from Disk to PowerPoint in C#
To insert an image from your disk to a PowerPoint document, you can use the ISlide.Shapes.AppendEmbedImage() method. The method requires the following three parameters:
- ShapeType: The type of shape for the image, typically a rectangle.
- IImageData: The data corresponding to the image you want to insert.
- RectangleF: Defines the position and size of the image.
The steps to insert an image from disk to a PowerPoint slide are as follows:
- Create a Presentation object.
- Load a PowerPoint document.
- Load an image from disk using Image.FromFile() method.
- Add the image to the image collection of the document using Presentation.Images.Append() method.
- Create a RectangleF object to specify where the image will be placed and its dimensions.
- Add the image to a specific slide at the specified position using ISlide.Shapes.AppendEmbedImage() method.
- Save the document to a different PowerPoint file.
- C#
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace InsertImageFromDisk { class Program { static void Main(string[] args) { // Create a Presentation object Presentation presentation = new Presentation(); // Load a PowerPoint file presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx"); // Load an image from the given file path Image img = Image.FromFile("C:\\Users\\Administrator\\Desktop\\logo.png"); // Add the image to the image collection of the document IImageData image = presentation.Images.Append(img); // Get the image width and height int width = image.Width; int height = image.Height; // Specify the image position in the slide int x = 100; int y = 100; // Get a specific slide ISlide slide = presentation.Slides[0]; // Insert the image to the slide at the specified position RectangleF rect = new RectangleF(x, y, width, height); IEmbedImage imageShape = slide.Shapes.AppendEmbedImage(ShapeType.Rectangle, image, rect); // Set the line fill type of the image shape to none imageShape.Line.FillType = FillFormatType.None; // Save the presentation to a different file presentation.SaveToFile("InsertImage.pptx", FileFormat.Pptx2010); // Dispose resources presentation.Dispose(); } } }
Insert an Image from a URL to PowerPoint
An image from a URL can be downloaded and saved as a stream using the WebClient class. Once downloaded, it can be loaded as a System.Drawing.Image object using the Image.FromStream() method. Then, you can use the ISlide.Shapes.AppendEmbedImage() method to insert the image into the slide as an image shape.
The steps to insert an image from a URL to a PowerPoint slide are as follows:
- Create a Presentation object.
- Load a PowerPoint document.
- Read and convert an image from a URL into stream using WebClient class.
- Load the image from stream using Image.FromStream() method.
- Add the image to the image collection of the document using Presentation.Images.Append() method.
- Create a RectangleF object to specify where the image will be placed and its dimensions.
- Add the image to a specific slide at the specified position using ISlide.Shapes.AppendEmbedImage() method.
- Save the document to a different PowerPoint file.
- C#
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; using System.Net; namespace InsertImageFromUrl { class Program { static void Main(string[] args) { // Create a Presentation object Presentation presentation = new Presentation(); // Load a PowerPoint file presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx"); // Declare a variable Image img; // Read an image from a URL string imageUrl = "https://i.imgur.com/OygmMoN.png"; using (WebClient webClient = new WebClient()) { using (Stream stream = webClient.OpenRead(imageUrl)) { // Load the image from stream img = Image.FromStream(stream); } } // Add the image to the image collection of the document IImageData image = presentation.Images.Append(img); // Get the image width and height int width = image.Width; int height = image.Height; // Specify the image position in the slide int x = 100; int y = 100; // Get a specific slide ISlide slide = presentation.Slides[0]; // Insert the image to the slide at the specified position RectangleF rect = new RectangleF(x, y, width, height); IEmbedImage imageShape = slide.Shapes.AppendEmbedImage(ShapeType.Rectangle, image, rect); // Set the line fill type of the image shape to none imageShape.Line.FillType = FillFormatType.None; // Save the presentation to a different file presentation.SaveToFile("InsertImage.pptx", FileFormat.Pptx2010); // Dispose resources presentation.Dispose(); } } }
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.