在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. BZOJ-4195 NOI2015Day1T1 程序自动分析 并查集+离散化

    总的来说,这道题水的有点莫名奇妙,不过还好一次轻松A 4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 836 ...

  2. 【poj1088】 滑雪

    http://poj.org/problem?id=1088 (题目链接) 题意 给出一个矩阵,任意选择一个起点,每次只能向周围4个格子中的值比当前格子小的格子移动,求最多能移动多少步. Soluti ...

  3. RegexBuddy正则表达式工具

    RegexBuddy非常的好用,而且还能生成.net的代码. 我们在使用正则匹配时,毕竟.net提供的方法中,对于多行匹配就不能用单纯的正则去实现,而我们需要把它转换成相应的类库方法进行实现. 那么R ...

  4. Integer.valueOf(String) 方法之惑

    本文由 ImportNew - 靳禹 翻译自 stackoverflow.欢迎加入翻译小组.转载请见文末要求. 有个仁兄在 StackOverflow 上发起了一个问题,是这么问的: “ 我被下面的代 ...

  5. phpcms v9无法连接数据库服务器,请检查配置

    安装phpcms v9是数据库信息配置正确,但仍提示:无法连接数据库服务器,请检查配置 1.修改install/step5.tpl.php 127行为:'&dbpw='+escape($('# ...

  6. javafx实现饼图统计效果图

  7. javafx之CSS初探

    文档:http://www.haogongju.net/art/1807238 javafx中的css元素必须有-fx-前缀. 一.介绍 java8中新增了javafx.css开放了css相关api. ...

  8. HttpResponse对象

    为了响应客户端的请求,同样定义了代表响应的类:HttpResponse类,它也定义在命名空间System.Web下,提供向客户端响应的方法和属性. HttpResponse常用属性和方法 响应对象用于 ...

  9. 如何使用Unix/Linux grep命令——磨刀不误砍柴工系列

     http://man.linuxde.net/grep ---------------------------------------------------- 如何使用Unix/Linux gre ...

  10. chown命令

    改变一个文件的所有者和组 1.命令格式: chown [选项]... [所有者][:[组]] 文件... 例子: komiles@iUbuntu:~/study/wordpress$ lltotal ...