失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > C#将PPT文件转换成图片并轮播展示

C#将PPT文件转换成图片并轮播展示

时间:2020-03-30 11:31:11

相关推荐

C#将PPT文件转换成图片并轮播展示

本人小白一个,初次接触C#,各位大神还请多多点出不足,谢谢!

项目需求是:User将PPT文件上传后,公司大厅电视看板内容随即改变为上传PPT文件内容,并进行轮播显示

思路:1,实现将PPT文件打开并转换成图片保存

2,每次PPT文件内容大小不定,即转换成图片数量不定

3,转换成的图片名称不可一致,因为浏览器具有缓存功能

废话不说,上代码:

aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="One.aspx.cs" Inherits="First" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="refresh" content="5"><title></title><style type="text/css">html,body{height:100%;width:100%;margin:0px;padding:0px;}#maindiv{height:100%;width:100%;margin:0px;padding:0px;}</style>

//电视看板当然需要全屏显示,所以需要进行自适应设置<script type="text/javascript">window.onload = function () {var table = document.getElementById('table');table.style.width = document.documentElement.clientWidth + 'px';table.style.height = document.documentElement.clientHeight + 'px';}</script></head><body overflow:-Scroll;overflow-y:hidden;overflow-x:hidden><form id="form1" runat="server"><div id="table"><div runat ="server" id ="maindiv" style =" border-width:0px;"/> </div></form></body></html>

后台代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.Security;using System.Timers;using System.Threading;using System.IO;public partial class First : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e){DirectoryInfo dirInfo = new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory + "1Img\\");FileInfo[] files = dirInfo.GetFiles();int length = files.Length;//获取文件间中的文件数量if (Session["f"] == null){Session["f"] = 0;}//创建image控件Session["f"] = int.Parse(Session["f"].ToString()) + 1;this.maindiv.Controls.Clear();Image image = new Image();foreach (var file in files)image.ImageUrl = "1Img/" + file.ToString().Substring(0, 17) + Session["f"].ToString() + ".png";image.ID = "" + Session["f"].ToString() + "image";image.Style["width"] = "100%";image.Style["height"] = "100%";this.maindiv.Controls.Add(image);if (int.Parse(Session["f"].ToString()) >= length){Session["f"] = 0;}} }

以上代码实现的是看板抓取指定文件夹中的图片Show到电视看板上

下面代码为将上传的PPT文件转换成图片保存如指定文件中:

aspx代码:

(引用<iframe>标签,方便User上传文件后,可以直接在当前页面看到上传后的内容是否正确)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Maintain.aspx.cs" Inherits="Maintain" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head runat="server"><meta http-equiv="refresh" content="30"><title>Maintain</title><style type="text/css">body,html,div{margin: 0px;padding:0px;}#all,span{width: 90%;height: 90%;padding:5% 5% 0 5%;}iframe{width:32%;}#form1{}</style><script type="text/javascript">window.onload = function () {var href = window.location.href.toString();var The = href.charAt(11);if (The == '9') {this.location.href = 'http://10.99.106.8:8080';}else {this.location.href = 'http://172.31.48.8:8080';}console.log(href)}</script></head><body><form action="" method="post" runat="server"><div id="all"><iframe src="http://10.99.106.211/WYF/One.aspx" frameborder="0" height="270px" ></iframe><iframe src="http://10.99.106.211/WYF/Two.aspx" frameborder="0" height="270px" ></iframe><iframe src="http://10.99.106.211/WYF/Three.aspx" frameborder="0" height="270px" ></iframe><iframe src="http://10.99.106.211/WYF/Four.aspx" frameborder="0" height="270px" ></iframe><iframe src="http://10.99.106.211/WYF/Five.aspx" frameborder="0" height="270px" ></iframe><iframe src="http://10.99.106.211/WYF/Six.aspx" frameborder="0" height="270px" ></iframe></div><div align="center"><asp:FileUpload ID="FileUpload1" runat="server" BorderColor="#FF3399" Font-Size="20px"/><asp:Button ID="Button1" runat="server" Text="上传" οnclick="Button1_Click"style="width:100px;height:40px" Font-Size="20px"/><asp:Label ID="Label1" runat="server" Font-Size="20px" ForeColor="Red" Text=""></asp:Label> </div></form></body></html>

后台代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Threading;using System.IO;public partial class Maintain : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}protected void Button1_Click(object sender, EventArgs e){if (FileUpload1.HasFile){string path = System.AppDomain.CurrentDomain.BaseDirectory + "Img\\";if (!(FileUpload1.FileName.EndsWith(".pptx") || FileUpload1.FileName.EndsWith(".PPTX"))){Label1.Text = "上传文件只能是png格式,请重新上传!";return;}//截取上传的文件名string filename = FileUpload1.FileName;string File = filename.Substring(0, 1);//上传之前删除指定该文件中的文件:DirectoryInfo dirInfo = new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory + "" + File + "Img\\");FileInfo[] files = dirInfo.GetFiles();// 获取该目录下的所有文件foreach (FileInfo file in files) {file.Delete();}//将上传文件保存至指定文件夹中FileUpload1.SaveAs(Server.MapPath("~/Img/") + FileUpload1.FileName);Label1.Text = "上传成功!";//将上传的文件转换成图片string pptPath = System.AppDomain.CurrentDomain.BaseDirectory + "Img\\" + File + ".pptx";//PPT文件的路径string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + "" + File + "Img\\";//转换成图片保存的路径Thread.Sleep(2000);var app = new Microsoft.Office.Interop.PowerPoint.Application();var ppt = app.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);var index = 0;var fileName = System.IO.Path.GetFileNameWithoutExtension(pptPath);foreach (Microsoft.Office.Interop.PowerPoint.Slide slid in ppt.Slides){++index;slid.Export(imgPath + string.Format(DateTime.Now.ToString("yyyy-MM-dd") +"{0}.png", index.ToString()), "png", 1024, 768);}//释放资源ppt.Close();app.Quit();GC.Collect();}else {Label1.Text = "未上传文件,请确认!!";return;}}}

本地运行后,不出意外,应该可以成功,但当发布到IIS上后,可能会出现报错:PPT文件无法打开

这个问题搞了好久,百度了很多,添加什么引用啊什么都无效,最后更换其他服务进行发布,最后成功了!

如果觉得《C#将PPT文件转换成图片并轮播展示》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。