Learn how to extract images from PowerPoint presentations in C#: https://blog.aspose.com/2022/01/13/extract-images-from-ppt-in-csharp-net/
Last active
February 1, 2022 12:02
-
-
Save aspose-com-gists/e38c11d0c0dc6fec2334dc1faf13babc to your computer and use it in GitHub Desktop.
Extract Images from PowerPoint PPT in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the presentation | |
Presentation pres = new Presentation("presentation.pptx"); | |
Aspose.Slides.IPPImage backImg = null; | |
int slideIndex = 0; | |
String imageType = ""; | |
for (int i = 0; i < pres.Slides.Count; i++) | |
{ | |
slideIndex++; | |
// Access the slide | |
ISlide slide = pres.Slides[i]; | |
System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg; | |
// Check if background has picture | |
if (slide.Background.FillFormat.FillType == FillType.Picture) | |
{ | |
// Get picture | |
backImg = slide.Background.FillFormat.PictureFillFormat.Picture.Image; | |
// Set picture format | |
imageType = backImg.ContentType; | |
imageType = imageType.Remove(0, imageType.IndexOf("/") + 1); | |
format = GetImageFormat(imageType); | |
// Save image | |
String imagePath = "BackImage_"; | |
backImg.SystemImage.Save(imagePath + "Slide_" + slideIndex.ToString() + "." + imageType, format); | |
} | |
else | |
{ | |
if (slide.LayoutSlide.Background.FillFormat.FillType == FillType.Picture) | |
{ | |
// Get background picture | |
backImg = slide.LayoutSlide.Background.FillFormat.PictureFillFormat.Picture.Image; | |
// Set picture format | |
imageType = backImg.ContentType; | |
imageType = imageType.Remove(0, imageType.IndexOf("/") + 1); | |
format = GetImageFormat(imageType); | |
// Save image | |
String imagePath = "BackImage_Slide_" + i; | |
backImg.SystemImage.Save(imagePath + "LayoutSlide_" + slideIndex.ToString() + "." + imageType, format); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the presentation | |
Presentation pres = new Presentation("presentation.pptx"); | |
Aspose.Slides.IPPImage img = null; | |
ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg; | |
int imageIndex = 1; | |
string imageType = ""; | |
String imagePath = "Image_"; | |
// Loop through images | |
foreach (var image in pres.Images) | |
{ | |
// Get image format | |
format = GetImageFormat(image.ContentType); | |
// Get image type | |
imageType = image.ContentType; | |
imageType = imageType.Remove(0, imageType.IndexOf("/") + 1); | |
// Save image | |
image.SystemImage.Save(imagePath + "Slide_" + imageIndex.ToString() + "." + imageType, format); | |
imageIndex++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the presentation | |
Presentation pres = new Presentation("presentation.pptx"); | |
Aspose.Slides.IPPImage img = null; | |
int slideIndex = 0; | |
String imageType = ""; | |
bool isImageFound = false; | |
// Loop through slides | |
for (int i = 0; i < pres.Slides.Count; i++) | |
{ | |
slideIndex++; | |
// Access the slide | |
ISlide slide = pres.Slides[i]; | |
System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg; | |
// Loop through the shapes | |
for (int j = 0; j < slide.Shapes.Count; j++) | |
{ | |
// Access the shape | |
IShape sh = slide.Shapes[j]; | |
// Check is it is an auto shape | |
if (sh is AutoShape) | |
{ | |
AutoShape ashp = (AutoShape)sh; | |
// Check if it has picture | |
if (ashp.FillFormat.FillType == FillType.Picture) | |
{ | |
// Get image | |
img = ashp.FillFormat.PictureFillFormat.Picture.Image; | |
imageType = img.ContentType; | |
imageType = imageType.Remove(0, imageType.IndexOf("/") + 1); | |
isImageFound = true; | |
} | |
} | |
else if (sh is PictureFrame) | |
{ | |
// If shape is a picture frame | |
IPictureFrame pf = (IPictureFrame)sh; | |
// Check if it contains a picture | |
if (pf.FillFormat.FillType == FillType.Picture) | |
{ | |
// Get image | |
img = pf.PictureFormat.Picture.Image; | |
imageType = img.ContentType; | |
imageType = imageType.Remove(0, imageType.IndexOf("/") + 1); | |
isImageFound = true; | |
} | |
} | |
// If image is found then save it | |
if (isImageFound) | |
{ | |
format = GetImageFormat(imageType); | |
String imagePath = "Image_"; | |
img.SystemImage.Save(imagePath + "Slide_" + slideIndex.ToString() + "_Shape_" + j.ToString() + "." + imageType, format); | |
} | |
isImageFound = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static System.Drawing.Imaging.ImageFormat GetImageFormat(String ImageType) | |
{ | |
System.Drawing.Imaging.ImageFormat Format = System.Drawing.Imaging.ImageFormat.Jpeg; | |
switch (ImageType) | |
{ | |
case "jpeg": | |
Format = System.Drawing.Imaging.ImageFormat.Jpeg; | |
break; | |
case "emf": | |
Format = System.Drawing.Imaging.ImageFormat.Emf; | |
break; | |
case "bmp": | |
Format = System.Drawing.Imaging.ImageFormat.Bmp; | |
break; | |
case "png": | |
Format = System.Drawing.Imaging.ImageFormat.Png; | |
break; | |
case "wmf": | |
Format = System.Drawing.Imaging.ImageFormat.Wmf; | |
break; | |
case "gif": | |
Format = System.Drawing.Imaging.ImageFormat.Gif; | |
break; | |
} | |
return Format; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment