将形状渲染为幻灯片图像
Contents
[
Hide
]
这包括两个主要功能:
- 从形状提取图像到文件。
- 将形状提取为图像文件。
从形状提取图像到文件
图像可以添加到幻灯片的背景和形状中。有时需要提取幻灯片形状中添加的图像。
在 Aspose.Slides for .NET 中,图像可以添加到幻灯片形状和幻灯片背景。图像存放在演示文稿的 ImageCollectionEx 中。在本例中,我们将遍历演示文稿每个幻灯片中的每个形状,检查是否有图像添加到形状中。如果在任何形状中找到图像,我们将提取该图像并保存为文件。下面的代码片段即可实现此目的。
//访问演示文稿
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++;
//访问第一张幻灯片
SlideEx sl = pres.Slides[i];
System.Drawing.Imaging.ImageFormat Format = System.Drawing.Imaging.ImageFormat.Jpeg;
for (int j = 0; j < sl.Shapes.Count; j++)
{
// 访问带图片的形状
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;
}
}
//
//设置所需的图片格式
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
Extract Shapes as Image Files
//实例化表示 PPT 文件的 Presentation 对象
Presentation pres = new Presentation("RenderShapeAsImage.ppt");
//使用幻灯片位置访问幻灯片
ISlide slide = pres.Slides[2];
for (int i = 0; i < slide.Shapes.Count; i++)
{
IShape shape = slide.Shapes[i];
//获取形状的缩略图像
using (IImage image = shape.GetImage(ShapeThumbnailBounds.Shape, 1.0f, 1.0f))
{
//以 gif 格式保存缩略图像
image.Save(i + ".gif", ImageFormat.Gif);
}
}
注意: 当前仅在 .ppt 文件中支持形状提取。