Sometimes, audiences may feel bored when they see slides with only text and images. Inserting a video is a great way to add visual interest to your presentation and make it more engaging for your audience. In this article, you will learn how to insert videos as well as replace and extract videos in PowerPoint in C# and VB.NET using Spire.Presentation for .NET.
- Insert a Video into a PowerPoint Presentation
- Replace a Video in a PowerPoint Presentation
- Extract Videos from a PowerPoint Presentation
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 a Video into a PowerPoint Presentation in C# and VB.NET
By inserting a video into your presentation, you can share the video with your audience instantly without having to look for it on your computer while presenting. The following steps demonstrate how to insert a video into a PowerPoint presentation:
- Create an instance of the Presentation class.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get a specific slide by its index through Presentation.Slides[int] property.
- Create an instance of the RectangleF class.
- Add a video to the slide using ISlide.Shapes.AppendVideoMedia(string, RectangleF) method.
- Set a thumbnail image for the video through IVideo.PictureFill.Picture.Url property.
- Save the result document using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace InsertVideoInPPT { class Program { static void Main(string[] args) { //Create a Presentation instance Presentation presentation = new Presentation(); //Load a PowerPoint document presentation.LoadFromFile(@"Input.pptx"); //Get the first slide ISlide slide = presentation.Slides[0]; //Add description text RectangleF rec_title = new RectangleF(50, 280, 160, 50); IAutoShape shape_title = slide.Shapes.AppendShape(ShapeType.Rectangle, rec_title); shape_title.ShapeStyle.LineColor.Color = Color.Transparent; shape_title.Fill.FillType = FillFormatType.None; TextParagraph para_title = new TextParagraph(); para_title.Text = "Video:"; para_title.Alignment = TextAlignmentType.Center; para_title.TextRanges[0].LatinFont = new TextFont("Myriad Pro Light"); para_title.TextRanges[0].FontHeight = 32; para_title.TextRanges[0].IsBold = TriState.True; para_title.TextRanges[0].Fill.FillType = FillFormatType.Solid; para_title.TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(68, 68, 68); shape_title.TextFrame.Paragraphs.Append(para_title); //Add a video to the first slide RectangleF videoRect = new RectangleF(presentation.SlideSize.Size.Width / 2 - 125, 240, 240, 130); IVideo video = slide.Shapes.AppendVideoMedia("Video.mp4", videoRect); //Set a thumbnail image for the video video.PictureFill.Picture.Url = @"Picture.png"; //Save the result document presentation.SaveToFile("InsertVideo.pptx", FileFormat.Pptx2010); } } }
Replace a Video in a PowerPoint Presentation in C# and VB.NET
If you think an existing video cannot support your statements well, you can replace it with another suitable one. The following steps demonstrate how to replace an existing video with another video in a PowerPoint presentation:
- Create an instance of the Presentation class.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get a specific slide by its index through Presentation.Slides[int] property.
- Load a video into a byte array using File.ReadAllBytes() method.
- Add the loaded video to the video collection of the document using Presentation.Videos.Append(byte[]) method.
- Loop through all shapes on the slide and find the video shape.
- Replace the original video with the loaded video through IVideo.EmbeddedVideoData property. Then change the thumbnail image of the video through IVideo.PictureFill.Picture.Url property.
- Save the result document using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; using System.IO; namespace ReplaceVideoInPPT { class Program { static void Main(string[] args) { //Create a Presentation instance Presentation ppt = new Presentation(); //Load a PowerPoint document ppt.LoadFromFile("InsertVideo.pptx"); //Get the first slide ISlide slide = ppt.Slides[0]; //Load a video into a byte array byte[] bts = File.ReadAllBytes(@"NewVideo.mp4"); //Add the loaded video to the video collection of the document VideoData videoData = ppt.Videos.Append(bts); //Loop through all shapes on the first slide foreach (Shape shape in slide.Shapes) { //Check if the shape is of IVideo type if (shape is IVideo) { //Typecast the shape as IVideo IVideo video = shape as IVideo; //Replace the original video with the loaded video video.EmbeddedVideoData = videoData; //Change the thumbnail image of the video video.PictureFill.Picture.Url = @"Picture1.png"; } } //Save the result document ppt.SaveToFile("ReplaceVideo.pptx", FileFormat.Pptx2010); } } }
Extract Videos from a PowerPoint Presentation in C# and VB.NET
If you like the videos in a PowerPoint presentation and want to use them in other places, you can extract and save them to your disk. The following steps demonstrate how to extract videos from a PowerPoint presentation:
- Create an instance of the Presentation class.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Loop through all slides in the document.
- Loop through all shapes on each slide.
- Find the video shapes, then save the videos to disk using IVideo.EmbeddedVideoData.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation; namespace ExtractVideosInPPT { class Program { static void Main(string[] args) { //Create a Presentation instance Presentation presentation = new Presentation(); //Load a PowerPoint document presentation.LoadFromFile(@"ReplaceVideo.pptx"); int i = 0; //Specify the output file path string result = string.Format(@"Videos\Video{0}.mp4", i); //Loop through all slides in the document foreach (ISlide slide in presentation.Slides) { //Loop through all shapes on each slide foreach (IShape shape in slide.Shapes) { //Check if the shape is of IVideo type if (shape is IVideo) { //Save the video to the specified path (shape as IVideo).EmbeddedVideoData.SaveToFile(result); i++; } } } } } }
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.