• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#向PPT文档插入图片以及导出图片的实例

C#向PPT文档插入图片以及导出图片的实例

作者:Yesi 字体:[增加 减小] 来源:互联网 时间:2017-05-28

Yesi 通过本文主要向大家介绍了c站,c语言,欲情 c max,维生素c,crh2c等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

 PowerPoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是PowerPoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版PowerPoint组件—Free Spire.Presentation,以C#/VB.NET编程的方式来快速地实现这两个功能。我们可以从官网下载Free Spire.Presentation,创建项目后添加此DLL作为引用。

插入图片

向PPT文档插入图片时,这里我选择插入两张图片到不同的两张幻灯片中。

具体步骤:

在之前需要添加以下命名空间:

using Spire.Presentation;
using Spire.Presentation.Drawing;
</div>

步骤1:新建一个PPT文档。

Presentation presentation = new Presentation();

presentation.Slides.Append();
</div>

步骤2:插入第一张图片到第一张幻灯片

string ImageFile = @"C:\Users\Administrator\Pictures\01.jpg";
RectangleF rect = new RectangleF(350, 100, 300, 250);  
presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;
</div>

步骤3:添加形状,再添加文本到形状里面。

RectangleF rect2 = new RectangleF(50, 100, 300, 250);
IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rect2);
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White;

//添加文本到形状中
shape.TextFrame.Text = "大熊猫是哺乳动物,已在地球上生存了至少800万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足1600只,属于中国国家一级保护动物。";
TextRange textRange = shape.TextFrame.TextRange;
shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;


//设置文本字体
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.Black;
textRange.LatinFont = new TextFont("Arial Black"

</div>

步骤4:同样,插入第二张图片到第二张幻灯片,添加形状,再添加文本到形状里面。最后保存文档。

presentation.SaveToFile(@"C:\Users\Administrator\Desktop\result.pptx ", FileFormat.Pptx2010);
System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\result.pptx ");
</div>

效果图:

 

全部代码:

 using System;
 using System.Drawing;
 using System.Windows.Forms;
 using Spire.Presentation;
 using Spire.Presentation.Drawing;
 
 namespace InsertimageinPowerPointFille
 {
  public partial class Form : Form
  {
   public Form()
   {
    InitializeComponent();
   }
 
   private void button_Click(object sender, EventArgs e)
   {
    //新建PPT
    Presentation presentation = new Presentation();
    presentation.Slides.Append();
 
    //插入第一张图片到第一张幻灯片
    string ImageFile = @"C:\Users\Administrator\Pictures\.jpg";
    RectangleF rect = new RectangleF(, , , );
    presentation.Slides[].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
    presentation.Slides[].Shapes[].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;
 
    //添加形状
    RectangleF rect = new RectangleF(, , , );
    IAutoShape shape = presentation.Slides[].Shapes.AppendShape(ShapeType.Rectangle, rect);
    shape.Fill.FillType = FillFormatType.None;
    shape.ShapeStyle.LineColor.Color = Color.White;
 
    //添加文本到形状中
    shape.TextFrame.Text = "大熊猫是哺乳动物,已在地球上生存了至少万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足只,属于中国国家一级保护动物。";
    TextRange textRange = shape.TextFrame.TextRange;
    shape.TextFrame.Paragraphs[].Alignment = TextAlignmentType.Left;
 
    //设置文本字体
    textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
    textRange.Fill.SolidColor.Color = Color.Black;
    textRange.LatinFont = new TextFont("Arial Black");
 
    //插入第二张图片到第二张幻灯片
    string ImageFile = @"C:\Users\Administrator\Pictures\.jpg";
    RectangleF rect = new RectangleF(, , , );
    presentation.Slides[].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
    presentation.Slides[].Shapes[].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;
 
    //添加形状
    RectangleF rect = new RectangleF(, , , );
    IAutoShape shape = presentation.Slides[].Shapes.AppendShape(ShapeType.Rectangle, rect);
    shape.Fill.FillType = FillFormatType.Solid;
    shape.Fill.FillType = FillFormatType.None;
    shape.ShapeStyle.LineColor.Color = Color.White;
 
    //添加文本到形状中
    shape.TextFrame.Text = "黑白相间的外表,有利隐蔽在密林的树上和积雪的地面而不易被天敌发现。相对锋利的爪和发达有力的前后肢,有利于大熊猫能快速爬上高大的乔木。";
    TextRange textRange = shape.TextFrame.TextRange;
 
    //设置文本字体
    textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
    textRange.Fill.SolidColor.Color = Color.Blue;
    textRange.LatinFont = new TextFont("Arial Black");
 
    //保存文件
    presentation.SaveToFile(@"C:\Users\Administrator\Desktop\result.pptx ", FileFormat.Pptx);
    System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\result.pptx ");
   }
  }
 }

</div>

从上面的代码可以发现,其实通过这个组件,我们还可以自由地设置我们想要的形状、文本、字体、颜色等等,用起来确实方便又快速。感兴趣的话可以试一下其他丰富的效果。

导出图片

现在,我们导出上述运行后文档的图片。

具体步骤:

同样添加如下命名空间:

using Spire.Presentation;
</div>

步骤1: 新建一个Presentation对象,并加载Presentation文件。

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\result.pptx");
</div>

 步骤2:遍历PPT文档所有的图片,并保存为.png格式。

for (int i = 0; i < ppt.Images.Count; i++)

   {

    Image image = ppt.Images[i].Image;

    image.Save(string.Format(@"..\..\Images{0}.png", i));

   }
</div>

效果图:

 

全部代码:

 using System;
 using System.Drawing;
 using System.Windows.Forms;
 using Spire.Presentation;
 
 namespace ExtractImagesfromPPT
 {
  public partial class Form : Form
  {
   public Form()
   {
    InitializeComponent();
   }
 
   private void button_Click(object sender, EventArgs e)
   {
    Presentation ppt = new Presentation();
    ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\result.pptx");
    for (int i = ; i < ppt.Images.Count; i++)
    {
     Image image = ppt.Images[i].Image;
     image.Save(string.Format(@"..\..\Images{}.png", i));
 
    }
   }
  }
 }
</div>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C# 检索不区分大小写并高亮显示实例详解
  • C#实现Base64处理的加密解密,编码解码示例
  • C# SqlHelper应用开发学习
  • C#多线程经典示例(吃苹果)
  • C#使用Windows Service的简单教程(创建、安装、卸载、调试)
  • C# 6.0 的知识梳理
  • C#向PPT文档插入图片以及导出图片的实例
  • C#使用Jquery zTree实现树状结构显示 异步数据加载
  • C#清理非托管对象实例分析
  • C#双缓冲技术实例详解

相关文章

  • 2017-05-28C#实现DataTable转换成IList的方法
  • 2017-05-28C#判断某程序是否运行的方法
  • 2017-05-28C#对象为Null模式(Null Object Pattern)实例教程
  • 2017-05-28C#编程自学之数据类型和变量一
  • 2017-05-28C#中事件的继承实例分析
  • 2017-05-28c#读写ini配置文件示例
  • 2017-05-28配置C#的系统环境变量的方法
  • 2017-05-28C#.NET获取拨号连接的宽带连接方法
  • 2017-05-28C#基础之匿名方法实例教程
  • 2017-05-28.Net(c#)汉字和Unicode编码互相转换实例

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#实现PDF文件添加图片背景
    • C#图像处理之图像目标质心检测的方法
    • C#特性 匿名类型与隐式类型局部变量使用介绍
    • C#中反射和扩展方法如何运用
    • 在C#使用字典存储事件示例及实现自定义事件访问器
    • des加密解密源码 C# key值问题分析
    • C#开发微信公众号接口开发
    • C#实现延时并自动关闭MessageBox的方法
    • C#实现洗牌算法
    • C#线程间不能调用剪切板的解决方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有