C# 如何添加PPT背景(纯色背景、渐变色背景、图片背景)

简介: 我们在创建Powerpoint文档时,系统默认的幻灯片是空白背景的,很多时候我们需要自定义幻灯片背景,以达到美观的文档效果。在下面的示例中将介绍给PowerPoint幻灯片设置背景的方法,主要包含以下三个部分:添加纯色背景添加渐变色背景添加图片作为背景所需工具Free Spire.

我们在创建Powerpoint文档时,系统默认的幻灯片是空白背景的,很多时候我们需要自定义幻灯片背景,以达到美观的文档效果。在下面的示例中将介绍给PowerPoint幻灯片设置背景的方法,主要包含以下三个部分:

  • 添加纯色背景
  • 添加渐变色背景
  • 添加图片作为背景

所需工具

示例代码(供参考)

步骤 1 :添加如下using指令

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

步骤 2 :创建文档

Presentation ppt = new Presentation();
ppt.LoadFromFile("test.pptx");

步骤 3 :添加纯色背景

//设置文档的背景填充模式为纯色填充
ppt.Slides[0].SlideBackground.Type = BackgroundType.Custom;
ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Solid;
ppt.Slides[0].SlideBackground.Fill.SolidColor.Color = Color.Pink;

步骤 4 :添加渐变背景色

//设置文档的背景填充模式为渐变色填充
ppt.Slides[1].SlideBackground.Type = BackgroundType.Custom;
ppt.Slides[1].SlideBackground.Fill.FillType = FillFormatType.Gradient;
ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(0f, KnownColors.Yellow);
ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(1f, KnownColors.Orange);

步骤 5 :添加图片作为背景

//设置幻灯片背景色为图片背景
ppt.Slides[2].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
ppt.Slides[2].SlideBackground.Fill.FillType = FillFormatType.Picture;
ppt.Slides[2].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
//加载图片作为幻灯片背景
Image img = Image.FromFile("green.png");
IImageData image = ppt.Images.Append(img);
ppt.Slides[2].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

步骤6 :保存文件

ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");

完成代码后,调试运行程序,生成文件,如下:

 

全部代码:

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace AddBackground_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Presentation类,加载PowerPoint文档
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");

            //设置文档的背景填充模式为纯色填充
            ppt.Slides[0].SlideBackground.Type = BackgroundType.Custom;
            ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Solid;
            ppt.Slides[0].SlideBackground.Fill.SolidColor.Color = Color.Pink;

            //设置文档的背景填充模式为渐变色填充
            ppt.Slides[1].SlideBackground.Type = BackgroundType.Custom;
            ppt.Slides[1].SlideBackground.Fill.FillType = FillFormatType.Gradient;
            ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(0f, KnownColors.Yellow);
            ppt.Slides[1].SlideBackground.Fill.Gradient.GradientStops.Append(1f, KnownColors.Orange);

            //设置幻灯片背景色为图片背景
            ppt.Slides[2].SlideBackground.Type = Spire.Presentation.Drawing.BackgroundType.Custom;
            ppt.Slides[2].SlideBackground.Fill.FillType = FillFormatType.Picture;
            ppt.Slides[2].SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
            //加载图片作为幻灯片背景
            Image img = Image.FromFile("green.png");
            IImageData image = ppt.Images.Append(img);
            ppt.Slides[2].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;

            //保存并打开文档
            ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("result.pptx");
        }
    }
}
View Code

本文完。

如需转载,请注明出处!!

目录
相关文章
|
测试技术 C# Windows
C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试
原文:C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试 WPF 没有用到 PictureBox, 而是用Image代替. 下面我试着加载显示一个图片 。 XAML CS Attempt 1: ImageMy_Image=newImage(Openfile.
1765 0
|
30天前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
6月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
260 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
|
4月前
|
API C#
C# 调用系统“API“设置图片为“桌面壁纸“
C# 调用系统“API“设置图片为“桌面壁纸“
|
6月前
|
C#
C# 图片RGB处理判断
C# 图片RGB处理判断 需要:根据一张原始图的RGB平均值和新的图片的RGB平均值的差距,来判断图中是否出现除原图中物体外的其他物体 前提:.Net framework 4.8 及以上 示例代码: 程序集:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imagin...
21 0
|
9月前
|
人工智能 文字识别 API
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
|
算法 定位技术 C#
C#开发:不规则裁切图片
C#开发:不规则裁切图片
120 0
|
区块链 C#
C#实现把图片转换为ico格式
C#实现把图片转换为ico格式
592 0
|
C# 图形学
C#裁剪图片的方法
C#裁剪图片的方法
255 0
如何在 C#中的listView 控件中显示图片?
如何在 C#中的listView 控件中显示图片?
959 0
如何在 C#中的listView 控件中显示图片?