.net mvc 上传PPT文件转化为图片解决方案

本文介绍了一个将PowerPoint演示文稿转换为一系列图片的.NET应用程序示例。该程序使用Microsoft Office Interop库来打开PPT文件,并将其每一页导出为800x600像素的JPG图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后台代码:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.Threading;
using System.IO;


namespace ppt2imageDemo.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/


        public ActionResult Index()
        {
            return View();
        }


        public JsonResult PushFilesAjax() {
            var file = Request.Files[0];
            var path = AppDomain.CurrentDomain.BaseDirectory + "imagesfromppt/" + file.FileName;
            var savepath = AppDomain.CurrentDomain.BaseDirectory + "ppt/";


            if(!System.IO.Directory.Exists(path)){
                System.IO.Directory.CreateDirectory(path);
            }
            if (!System.IO.Directory.Exists(savepath))
            {
                System.IO.Directory.CreateDirectory(savepath);
            }
            var filepath = Path.Combine(savepath, file.FileName);
            file.SaveAs(filepath);
            Microsoft.Office.Interop.PowerPoint.Application application = null;
            Presentation persentation = null;
            try
            {
                application = new Microsoft.Office.Interop.PowerPoint.Application();
                persentation = application.Presentations.Open(filepath);
                //persentation.Slides[1].Export(path + "\\page" + 1 + ".jpg", "JPG", 800, 600);
                for (var k = 1; k <= persentation.Slides.Count;k++ )
                {
                    persentation.Slides[k].Export(path + "\\page" + k + ".jpg", "JPG", 800, 600);
                }


            }
            catch(Exception e) {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return Json(new { success=true});
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值