C#将PPT文件转换成图片并轮播展示
本人小白一个,初次接触C#,各位大神还请多多点出不足,谢谢!
项目需求是:User将PPT文件上传后,公司大厅电视看板内容随即改变为上传PPT文件内容,并进行轮播显示
思路:1,实现将PPT文件打开并转换成图片保存
2,每次PPT文件内容大小不定,即转换成图片数量不定
3,转换成的图片名称不可一致,因为浏览器具有缓存功能
废话不说,上代码:
aspx代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="One.aspx.cs" Inherits="First" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head> 7 <meta http-equiv="refresh" content="5"> 8 <title></title> 9 <style type="text/css"> 10 html,body 11 { 12 height:100%; 13 width:100%; 14 margin:0px; 15 padding:0px; 16 } 17 #maindiv 18 { 19 height:100%; 20 width:100%; 21 margin:0px; 22 padding:0px; 23 24 } 25 </style> 26
27
28 //电视看板当然需要全屏显示,所以需要进行自适应设置 29 <script type="text/javascript"> 30 window.onload = function () { 31 var table = document.getElementById('table'); 32 table.style.width = document.documentElement.clientWidth + 'px'; 33 table.style.height = document.documentElement.clientHeight + 'px'; 34 } 35 </script> 36 </head> 37 <body overflow:-Scroll;overflow-y:hidden;overflow-x:hidden> 38 <form id="form1" runat="server"> 39 <div id="table"> 40 <div runat ="server" id ="maindiv" style =" border-width:0px;"/> 41 </div> 42 </form> 43 </body> 44 </html>
后台代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Web.Security; 8 using System.Timers; 9 using System.Threading; 10 using System.IO; 11 12 13 public partial class First : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 DirectoryInfo dirInfo = new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory + "1Img\\"); 18 FileInfo[] files = dirInfo.GetFiles(); 19 int length = files.Length; 20 //获取文件间中的文件数量 21 22 if (Session["f"] == null) 23 { 24 Session["f"] = 0; 25 } 26 //创建image控件 27 Session["f"] = int.Parse(Session["f"].ToString()) + 1; 28 this.maindiv.Controls.Clear(); 29 Image image = new Image(); 30 foreach (var file in files) 31 image.ImageUrl = "1Img/" + file.ToString().Substring(0, 17) + Session["f"].ToString() + ".png"; 32 image.ID = "" + Session["f"].ToString() + "image"; 33 image.Style["width"] = "100%"; 34 image.Style["height"] = "100%"; 35 this.maindiv.Controls.Add(image); 36 if (int.Parse(Session["f"].ToString()) >= length) 37 { 38 Session["f"] = 0; 39 } 40 } 41 }
以上代码实现的是看板抓取指定文件夹中的图片Show到电视看板上
下面代码为将上传的PPT文件转换成图片保存如指定文件中:
aspx代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Maintain.aspx.cs" Inherits="Maintain" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="refresh" content="30"> 8 <title>Maintain</title> 9 <style type="text/css"> 10 body,html,div{ 11 margin: 0px; 12 padding:0px; 13 } 14 #all,span{ 15 width: 90%; 16 height: 90%; 17 padding:5% 5% 0 5%; 18 } 19 iframe{ 20 width:32%; 21 } 22 #form1 23 { 24 25 } 26 </style> 27 <script type="text/javascript"> 28 window.onload = function () { 29 var href = window.location.href.toString(); 30 var The = href.charAt(11); 31 32 if (The == '9') { 33 34 this.location.href = 'http://10.99.106.8:8080'; 35 } 36 else { 37 38 this.location.href = 'http://172.31.48.8:8080'; 39 40 } 41 console.log(href) 42 } 43 </script> 44 </head> 45 <body> 46 <form action="" method="post" runat="server"> 47 <div id="all"> 48 <iframe src="http://10.99.106.211/WYF/One.aspx" frameborder="0" height="270px" ></iframe> 49 <iframe src="http://10.99.106.211/WYF/Two.aspx" frameborder="0" height="270px" ></iframe> 50 <iframe src="http://10.99.106.211/WYF/Three.aspx" frameborder="0" height="270px" ></iframe> 51 <iframe src="http://10.99.106.211/WYF/Four.aspx" frameborder="0" height="270px" ></iframe> 52 <iframe src="http://10.99.106.211/WYF/Five.aspx" frameborder="0" height="270px" ></iframe> 53 <iframe src="http://10.99.106.211/WYF/Six.aspx" frameborder="0" height="270px" ></iframe> 54 </div> 55 <div align="center"> 56 <asp:FileUpload ID="FileUpload1" runat="server" BorderColor="#FF3399" Font-Size="20px"/> 57 <asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click" 58 style="width:100px;height:40px" Font-Size="20px"/> 59 <asp:Label ID="Label1" runat="server" Font-Size="20px" ForeColor="Red" Text=""></asp:Label> 60 </div> 61 </form> 62 </body> 63 </html>
后台代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Threading; 8 using System.IO; 9 10 public partial class Maintain : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 15 } 16 17 protected void Button1_Click(object sender, EventArgs e) 18 { 19 if (FileUpload1.HasFile) 20 { 21 string path = System.AppDomain.CurrentDomain.BaseDirectory + "Img\\"; 22 if (!(FileUpload1.FileName.EndsWith(".pptx") || FileUpload1.FileName.EndsWith(".PPTX"))) 23 { 24 Label1.Text = "上传文件只能是png格式,请重新上传!"; 25 return; 26 } 27 //截取上传的文件名 28 string filename = FileUpload1.FileName; 29 string File = filename.Substring(0, 1); 30 //上传之前删除指定该文件中的文件: 31 DirectoryInfo dirInfo = new DirectoryInfo(System.AppDomain.CurrentDomain.BaseDirectory + "" + File + "Img\\"); 32 FileInfo[] files = dirInfo.GetFiles(); 33 // 获取该目录下的所有文件 34 foreach (FileInfo file in files) { 35 file.Delete(); 36 } 37 //将上传文件保存至指定文件夹中 38 FileUpload1.SaveAs(Server.MapPath("~/Img/") + FileUpload1.FileName); 39 Label1.Text = "上传成功!"; 40 //将上传的文件转换成图片 41 string pptPath = System.AppDomain.CurrentDomain.BaseDirectory + "Img\\" + File + ".pptx";//PPT文件的路径 42 string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + "" + File + "Img\\";//转换成图片保存的路径 43 Thread.Sleep(2000); 44 var app = new Microsoft.Office.Interop.PowerPoint.Application(); 45 var ppt = app.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); 46 var index = 0; 47 var fileName = System.IO.Path.GetFileNameWithoutExtension(pptPath); 48 foreach (Microsoft.Office.Interop.PowerPoint.Slide slid in ppt.Slides) 49 { 50 ++index; 51 slid.Export(imgPath + string.Format(DateTime.Now.ToString("yyyy-MM-dd") +"{0}.png", index.ToString()), "png", 1024, 768); 52 } 53 //释放资源 54 ppt.Close(); 55 app.Quit(); 56 GC.Collect(); 57 } 58 else { 59 Label1.Text = "未上传文件,请确认!!"; 60 return; 61 } 62 } 63 }
本地运行后,不出意外,应该可以成功,但当发布到IIS上后,可能会出现报错:PPT文件无法打开
这个问题搞了好久,百度了很多,添加什么引用啊什么都无效,最后更换其他服务进行发布,成功了!