本文介绍了从.PPTX文件打开XML获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Windows在.NET 4.0中窗体应用程序。我在C#中工作。
我想从一个.PPTX文件中给定的幻灯片抓取的图像
I have a Windows forms app in .Net 4.0. I work in C#.I want to grab an image from a given slide in a .pptx file.
此代码获取幻灯片上的每一个形象:
This code gets every image on the slide:
public static SlidePart GetSlidePart(PresentationDocument presentationDocument, int slideIndex)
{
if (presentationDocument == null)
{
throw new ArgumentNullException("presentationDocument", "GetSlidePart Method: parameter presentationDocument is null");
}
int slidesCount = CountSlides(presentationDocument);
if (slideIndex < 0 || slideIndex >= slidesCount)
{
throw new ArgumentOutOfRangeException("slideIndex", "GetSlidePart Method: parameter slideIndex is out of range");
}
PresentationPart presentationPart = presentationDocument.PresentationPart;
if (presentationPart != null && presentationPart.Presentation != null)
{
Presentation presentation = presentationPart.Presentation;
if (presentation.SlideIdList != null)
{
var slideIds = presentation.SlideIdList.ChildElements;
if (slideIndex < slideIds.Count)
{
string slidePartRelationshipId = (slideIds[slideIndex] as SlideId).RelationshipId;
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
return slidePart;
}
}
}
return null;// No slide found
}
不过,如何slidePart转换,这将显示在我的Windows窗体(在图像列表或类似的东西)?
But, how to convert slidePart to an image which will be shown in my Windows form (in imageList or something similar)?
推荐答案
找到了一个方法:
SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slidePartRelationshipId);
Slide slide = slidePart.Slide;
ImagePart imagePart = (ImagePart)slide.SlidePart.GetPartById("rId3");
System.Drawing.Image img = System.Drawing.Image.FromStream(imagePart.GetStream());
这篇关于从.PPTX文件打开XML获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!