zoukankan      html  css  js  c++  java
  • 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", "
    " + 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("路径为空或不存在!");
                }
            }
        }
    }
  • 相关阅读:
    stylelint 安装配置
    使用 jest 测试 react component 的配置,踩坑。
    互联网媒体类型 MIME Type
    react-router 父子路由同时要接收 params 的写法
    fixed 相对于父容器定位
    react 点击空白处隐藏弹出层
    canvas 使用 isPointInPath() 判断鼠标位置是否在绘制的元素上
    JavaScript 缓存基本原理
    简单说明 Virtual DOM 为啥快
    通过阻止 touchstart 事件,解决移动端的 BUG
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/7267539.html
Copyright © 2011-2022 走看看