Rendering Shapes on Slide as Images
Contents
[
Hide
]
This covers two main function:
- Extracting Image from Shape to file.
- Extracting Shapes as image file.
Extracting Image from Shape to file
Images are added in slide background and shapes. Sometimes, it is required to extract the images added in the presentation shapes.
In Aspose.Slides for .NET, images can be added to slide shape and slide background. The images are added in ImageCollectionEx of the presentation. In this example we will traverse through each shape inside every slide of presentation and see if there is any image added in slide shape. If the image will be found for any shape, we will extract that and will save it in file.The following code snippet will serve the purpose.
//Accessing the presentation
PresentationEx pres = new PresentationEx("RenderImageFromShape.pptx");
ImageEx img = null;
int slideIndex = 0;
String ImageType = "";
bool ifImageFound = false;
for (int i = 0; i < pres.Slides.Count; i++)
{
slideIndex++;
//Accessing the first slide
SlideEx sl = pres.Slides[i];
System.Drawing.Imaging.ImageFormat Format = System.Drawing.Imaging.ImageFormat.Jpeg;
for (int j = 0; j < sl.Shapes.Count; j++)
{
// Accessing the shape with picture
ShapeEx sh = sl.Shapes[j];
if (sh is AutoShapeEx)
{
AutoShapeEx ashp = (AutoShapeEx)sh;
if (ashp.FillFormat.FillType == FillTypeEx.Picture)
{
img = ashp.FillFormat.PictureFillFormat.Picture.Image;
ImageType = img.ContentType;
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
ifImageFound = true;
}
}
else if (sh is PictureFrameEx)
{
PictureFrameEx pf = (PictureFrameEx)sh;
if (pf.FillFormat.FillType == FillTypeEx.Picture)
{
img = pf.PictureFormat.Picture.Image;
ImageType = img.ContentType;
ImageType = ImageType.Remove(0, ImageType.IndexOf("/") + 1);
ifImageFound = true;
}
}
//
//Setting the desired picture format
if (ifImageFound)
{
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;
}
//
img.Image.Save(path+"ResultedImage"+"." + ImageType, Format);
}
ifImageFound = false;
Download Sample Code
Extracting Shapes as image file
//Instantiate the Presentation object that represents a PPT file
Presentation pres = new Presentation("RenderShapeAsImage.ppt");
//Accessing a slide using its slide position
ISlide slide = pres.Slides[2];
for (int i = 0; i < slide.Shapes.Count; i++)
{
IShape shape = slide.Shapes[i];
//Getting the thumbnail image of the shape
using (IImage image = shape.GetImage(ShapeThumbnailBounds.Shape, 1.0f, 1.0f))
{
//Saving the thumbnail image in gif format
image.Save(i + ".gif", ImageFormat.Gif);
}
}
*Note:*Extraction of shape is currently supported in .ppt file.