首页 > 代码库 > C#学习笔记(24)——C#将PPT批量转为JPG(最简单的方法)

C#学习笔记(24)——C#将PPT批量转为JPG(最简单的方法)

说明(2017-8-1 11:15:46):

1. 哈哈,我就说微软肯定有自己的办法,把PPT转成图片。

2. 主要是要引入两个微软自己的程序集,vs里自带直接添加引用,注意一下版本,12.0是office2007吧?反正我用的14.0的。

技术分享

技术分享

添加完会多出来两个引用:

技术分享

3. 剩下的看代码就可以了,非常之简单,核心代码就三行!

4. 感谢下面的参考资料:

http://bbs.csdn.net/topics/360019178

另外stackoverflow是个好东西:

https://stackoverflow.com/questions/2972263/ppt-slides-to-images

还有必应搜索,不知道比百度强到哪里去了,添加程序集的时候遇到几个问题,都是在必应里搜索到解决办法的:

http://cn.bing.com/

 

xaml:

<Window x:Class="PPT2JPG.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="开始转成图片" HorizontalAlignment="Left" Height="40" Margin="366,181,0,0" VerticalAlignment="Top" Width="103" Click="Button_Click_1"/>
        <Label Content="PPT路径" HorizontalAlignment="Left" Height="32" Margin="46,69,0,0" VerticalAlignment="Top" Width="77"/>
        <TextBox Name="txtBoxPath" HorizontalAlignment="Left" Height="35" Margin="129,66,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="354"/>
    </Grid>
</Window>

 

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.IO;


namespace PPT2JPG
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            File.Delete("log.txt");
        }

        private void ShowLog(string log)
        {
            File.AppendAllText("log.txt", "\r\n" + DateTime.Now.ToString() + log);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }
        //PPT转成JPG
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string pptPath = txtBoxPath.Text;
            if (pptPath != "" && Directory.Exists(pptPath))
            {
                DirectoryInfo pptInfos = new DirectoryInfo(pptPath);
                foreach (FileInfo pptInfo in pptInfos.GetFiles("*.ppt*"))
                {
                    //创建图片文件夹
                    if (!Directory.Exists(pptInfo.FullName.Split(.)[0]))
                    {
                        Directory.CreateDirectory(pptInfo.FullName.Split(.)[0]);
                    }
                    else
                    {
                        continue;
                    }
                    //开始转换PPT
                    try
                    {
                        ApplicationClass pptApp = new ApplicationClass();
                        Presentation pptPres = pptApp.Presentations.Open(pptInfo.FullName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
                        for (int i = 0; i < pptPres.Slides.Count; i++)
                        {
                            string imgPath = pptInfo.FullName.Split(.)[0] + @"\" + (i + 1).ToString() + ".jpg";
                            pptPres.Slides[i + 1].Export(imgPath, "jpg", 960, 720);
                        }
                        Console.WriteLine("已经转换完:"+pptInfo.FullName);
                        pptPres.Close();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("无法转换:" + pptInfo.FullName);
                        ShowLog("无法生成图片:" + pptInfo.FullName);
                        continue;
                    }

                }
                MessageBox.Show("转换图片完成!");
            }
            else
            {
                MessageBox.Show("路径为空或不存在!");
            }
        }
    }
}

 

C#学习笔记(24)——C#将PPT批量转为JPG(最简单的方法)