在Office下,PowerPoint可以直接把每张幻灯片转成图片,而Word不能直接保存图片。所以只能通过先转换成xps文件,然后再转成图片。

一、PPT 保存为图片

     /// <summary>
/// 将ppt转成图片
/// </summary>
/// <param name="fileName"></param>
private void SaveToImages(string fileName)
{
var presentation = _application.Presentations.Open(fileName, MsoTriState.msoFalse, MsoTriState.msoFalse,
MsoTriState.msoFalse); presentation.SaveAs(_path, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoTrue);
}
 

二、Word转成图片

      /// <summary>
/// Word转xps
/// </summary>
public string ConvertDocToXps(string filePath)
{
var application = new Microsoft.Office.Interop.Word.Application();
application.Documents.Add(filePath);
var name = System.IO.Path.GetFileNameWithoutExtension(filePath) + ".xps";
var path = System.IO.Path.Combine(_path, name);
try
{
application.ActiveDocument.ExportAsFixedFormat(path, WdExportFormat.wdExportFormatXPS);
}
catch (Exception)
{
return string.Empty;
}
finally
{
application.Documents.Close();
application.Quit();
}
return filePath;
}
 
     /// <summary>
/// xps 转jpg图片
/// </summary>
/// <param name="path"></param>
/// <param name="dirPath"></param>
/// <returns></returns>
public bool XpsToImages(string path, string dirPath)
{
if (string.IsNullOrEmpty(dirPath)) return false; if (dirPath[dirPath.Length - 1] != '\\')
dirPath += "\\"; if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath); var xpsDocument = new XpsDocument(path, FileAccess.Read);
FixedDocumentSequence document = xpsDocument.GetFixedDocumentSequence(); MemoryStream[] streams = null;
try
{ int pageCount = document.DocumentPaginator.PageCount;
DocumentPage[] pages = new DocumentPage[pageCount];
for (int i = 0; i < pageCount; i++)
pages[i] = document.DocumentPaginator.GetPage(i); streams = new MemoryStream[pages.Count()]; for (int i = 0; i < pages.Count(); i++)
{
DocumentPage source = pages[i];
streams[i] = new MemoryStream(); RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)source.Size.Width,
(int)source.Size.Height,
96d,
96d,
PixelFormats.Default); renderTarget.Render(source.Visual); JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 100;
encoder.Frames.Add(BitmapFrame.Create(renderTarget)); encoder.Save(streams[i]); FileStream file = new FileStream(dirPath + "Page_" + (i + 1) + ".jpg", FileMode.Create);
file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length);
file.Close(); streams[i].Position = 0;
}
}
catch (Exception e1)
{
return false;
}
finally
{
if (streams != null)
{
foreach (MemoryStream stream in streams)
{
stream.Close();
stream.Dispose();
}
}
} return true;
}

代码下载

 

WPF 将PPT,Word转成图片的更多相关文章

  1. C# Asp.Net 实现PPT/PDF转成图片(不依赖office)

    最近公司有个需求,将PPT课件转成图片列表,然后在前端展示成轮播图,于是一开始通过Microsoft.Office.Interop.PowerPoint包实现了这个需求具体代码如下: /// < ...

  2. pdf ppt word office转图片 教学白板

    https://zh-cn.libreoffice.org/ http://www.imagemagick.org/script/ 首先用libreoffice将ppt转换为pdf格式,然后再用con ...

  3. 使用Aspose.Words把 word转成图片

    Document doc = new Document("f:\\333.doc"); ImageSaveOptions iso = new ImageSaveOptions(Sa ...

  4. Java中Office(word/ppt/excel)转换成HTML实现

    运行条件:JDK + jacob.jar + jacob.dll 1) 把jacob.dll在 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin\ 以及C:\WINDOWS\sys ...

  5. PPT文件流转为图片,并压缩成ZIP文件输出到指定目录

    实现流程: 接收InputStream流->复制流->InputStream流转为PPT->PPT转为图片->所有图片压缩到一个压缩文件下 注意: 1.PPT文件分为2003和 ...

  6. WPF GDI+字符串绘制成图片(二)

    原文:WPF GDI+字符串绘制成图片(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...

  7. WPF GDI+字符串绘制成图片(一)

    原文:WPF GDI+字符串绘制成图片(一) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...

  8. WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片

    原文:WPF中实现图片文件转换成Visual对象,Viewport3D对象转换成图片 1.图片文件转换成Visual对象 private Visual CreateVisual(string imag ...

  9. Java 将 PPT 形状(表格、文本框、心形、图表等)保存成图片

    MS PowerPoint中的表格.文本框.心形.图表.图片等均可以称为形状,将这些形状保存成图片,便可分类储存,方便日后查找,再利用. 本文将介绍如何使用 Spire.Presentation fo ...

随机推荐

  1. Android Studio能干什么

    建立系统工具包可以用来生成,测试,运行您的应用程序和软件包.构建系统是独立于Android的工作室,所以你可以调用它的Android的工作室或从命令行.在你写你的应用程序,你可以使用编译系统的特点: ...

  2. GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决

    直入主题,首先声明,这个问题是无法解决的,特此在这说明 一.如何动态添加列,如下: 在页面重写OnInit事件,至于为什么要在这个事件写,根据页面的声明周期和经验可知(不用去别的地方找了,这个我找了之 ...

  3. 领域模型中的实体类分为四种类型:VO、DTO、DO、PO

    http://kb.cnblogs.com/page/522348/ 由于不同的项目和开发人员有不同的命名习惯,这里我首先对上述的概念进行一个简单描述,名字只是个标识,我们重点关注其概念: 概念: V ...

  4. ci中如何私有化方法

    私有方法 在某些情况下,你可能想要隐藏一些方法使之无法对外查阅.将方法私有化很简单,只要在方法名字前面加一个下划线("_")做前缀就无法通过 URL 访问到了.例如,如果你有一个像 ...

  5. tp auth 转载保存

    PS:最近需要做一个验证用户权限的功能,在官方和百度看了下,发现大家都是用auth来做验证,官方有很多auth的使用教程,但是都不全面,我也提问了几个关于auth的问题 也没人来回答我,无奈只好一步步 ...

  6. JavaScript中的位置坐标

    参考来源 http://www.cnblogs.com/tugenhua0707/p/4501843.html screenX.screenY:浏览器屏幕水平.垂直坐标(相对于浏览器内整个屏幕,包括地 ...

  7. C++_Eigen函数库用法笔记——Block Operations

    Using block operations rvalue, i.e. it was only read from lvalues, i.e. you can assign to a block Co ...

  8. WCF中的标准绑定

    使用过WCF的童鞋们都很清楚,绑定是必须的.我将这些绑定总结了下. 一.标准绑定简要说明 1.basicHttpBinding 基于WS-I Basic Profile 1.1 的web服务,所需的. ...

  9. oracle中Blob和Clob类型的区别

    BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...

  10. spring 注解的总结

    一.java内置注解 1.@Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: ElemenetType.CONSTRUCTOR   构造器声明 ElemenetTyp ...