博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 在PPT中绘制形状(shape)
阅读量:6245 次
发布时间:2019-06-22

本文共 7966 字,大约阅读时间需要 26 分钟。

概述

本篇文章将介绍C# 在PPT幻灯片中操作形状(shape)的方法。这里主要涉及常规形状,如箭头、矩形、圆形、三角形、多边形、不规则形状等。下面的示例中,可以通过绘制形状,并设置相应格式等。示例包含以下要点:

  • 绘制形状
  • 用图片填充形状
  • 在形状中添加文字
  • 设置形状单色、渐变色填充
  • 设置形状阴影效果
  • 组合多个形状为一个
  • 设置形状光边效果
  • 将形状保存为图片

 

工具

下载安装后,注意在程序中添加引用Spire.Presentation.dll到程序,dll文件可在安装路径下的Bin文件夹中获取。

示例代码(供参考)

【示例1】绘制形状

步骤1:新建一个幻灯片

//新建一个幻灯片文档,并指定幻灯片大小Presentation ppt = new Presentation();ppt.SlideSize.Type = SlideSizeType.Screen16x9;

步骤2:获取第一张幻灯片

ISlide slide = ppt.Slides[0];

步骤3:添加一个云朵形状,并填充渐变色,绘入文字

//添加一个云朵形状,并填充渐变颜色IAutoShape shape1 = slide.Shapes.AppendShape(ShapeType.CalloutCloud, new RectangleF(160, 50, 200, 80));shape1.Fill.FillType = FillFormatType.Gradient;shape1.Fill.Gradient.GradientStops.Append(0, Color.Blue);shape1.Fill.Gradient.GradientStops.Append(1, Color.Azure);shape1.Line.FillType = FillFormatType.None;//在形状中绘制文本,并设置字体、字号、字体颜色等shape1.AppendTextFrame("HOW??");TextRange textRange = (shape1 as IAutoShape).TextFrame.TextRange;textRange.FontHeight = 13;textRange.LatinFont = new TextFont("Arial");textRange.Fill.FillType = FillFormatType.Solid;textRange.Fill.SolidColor.Color = Color.White;

步骤4:添加椭圆形状,并加载图片填充

IAutoShape shape2 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(50, 130, 150, 250));string picPath = "sk.png"; shape2.Fill.FillType = FillFormatType.Picture;shape2.Fill.PictureFill.Picture.Url = picPath;shape2.Fill.PictureFill.FillType = PictureFillType.Stretch;shape2.Line.FillType = FillFormatType.None;

步骤5:添加三角形,并设置边框效果,阴影效果

//添加一个三角形,填充颜色并设置边框样式IAutoShape shape3 = slide.Shapes.AppendShape(ShapeType.Triangle, new RectangleF(480, 180, 100, 130));shape3.Fill.FillType = FillFormatType.Solid;shape3.Fill.SolidColor.Color = Color.Wheat;shape3.Line.Width = 3;shape3.Line.DashStyle = LineDashStyleType.Dash;shape3.ShapeStyle.LineColor.Color = Color.Red;//设置形状阴影效果PresetShadow presetShadow = new PresetShadow();presetShadow.Preset = PresetShadowValue.BackRightPerspective;presetShadow.ColorFormat.Color = Color.LightGray;shape3.EffectDag.PresetShadowEffect = presetShadow;

步骤6:添加一个带箭头的直线

IAutoShape shape4 = slide.Shapes.AppendShape(ShapeType.Line, new RectangleF(660, 200, 100, 100));shape4.ShapeStyle.LineColor.Color = Color.Red;shape4.Line.LineEndType = LineEndType.StealthArrow;shape4.Rotation = -90;//设置形状旋转角度

步骤7:绘制一个圆形、五角星,并设置光边效果,将拉个形状组合

//添加一个圆形IAutoShape shape5 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(289, 166, 120, 120));shape5.Fill.FillType = FillFormatType.Solid;shape5.Fill.SolidColor.Color = Color.White;shape5.Line.FillType = FillFormatType.Solid;shape5.Line.SolidFillColor.Color = Color.Red;//添加一个五角星形状IAutoShape shape6 = slide.Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(300, 170, 100, 100));shape6.Fill.FillType = FillFormatType.Solid;shape6.Fill.SolidColor.Color = Color.Orange;shape6.Line.FillType = FillFormatType.None;//设置五角星形状的光边效果GlowEffect glow = new GlowEffect();glow.ColorFormat.Color = Color.Yellow;glow.Radius = 7.0;shape6.EffectDag.GlowEffect = glow;//将shape5和shape6两个形状组合ArrayList list = new ArrayList();list.Add(shape5);list.Add(shape6);ppt.Slides[0].GroupShapes(list);

步骤8:保存文档

ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);

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

全部代码:

using Spire.Presentation;using Spire.Presentation.Drawing;using System.Collections;using System.Drawing;namespace DrawShape_PPT{    class Program    {        static void Main(string[] args)        {            //新建一个幻灯片文档,并指定幻灯片大小            Presentation ppt = new Presentation();            ppt.SlideSize.Type = SlideSizeType.Screen16x9;            //获取第一张幻灯片            ISlide slide = ppt.Slides[0];            //添加一个云朵形状,并填充渐变颜色            IAutoShape shape1 = slide.Shapes.AppendShape(ShapeType.CalloutCloud, new RectangleF(160, 50, 200, 80));            shape1.Fill.FillType = FillFormatType.Gradient;            shape1.Fill.Gradient.GradientStops.Append(0, Color.Blue);            shape1.Fill.Gradient.GradientStops.Append(1, Color.Azure);            shape1.Line.FillType = FillFormatType.None;            //在形状中绘制文本,并设置字体、字号、字体颜色等            shape1.AppendTextFrame("HOW??");            TextRange textRange = (shape1 as IAutoShape).TextFrame.TextRange;            textRange.FontHeight = 13;            textRange.LatinFont = new TextFont("Arial");            textRange.Fill.FillType = FillFormatType.Solid;            textRange.Fill.SolidColor.Color = Color.White;            //添加一个椭圆,并用图片填充形状            IAutoShape shape2 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(50, 130, 150, 250));            string picPath = "sk.png";             shape2.Fill.FillType = FillFormatType.Picture;            shape2.Fill.PictureFill.Picture.Url = picPath;            shape2.Fill.PictureFill.FillType = PictureFillType.Stretch;            shape2.Line.FillType = FillFormatType.None;            //添加一个三角形,填充颜色并设置形状边框样式            IAutoShape shape3 = slide.Shapes.AppendShape(ShapeType.Triangle, new RectangleF(480, 180, 100, 130));            shape3.Fill.FillType = FillFormatType.Solid;            shape3.Fill.SolidColor.Color = Color.Wheat;            shape3.Line.Width = 3;            shape3.Line.DashStyle = LineDashStyleType.Dash;            shape3.ShapeStyle.LineColor.Color = Color.Red;            //设置形状阴影效果            PresetShadow presetShadow = new PresetShadow();            presetShadow.Preset = PresetShadowValue.BackRightPerspective;            presetShadow.ColorFormat.Color = Color.LightGray;            shape3.EffectDag.PresetShadowEffect = presetShadow;                     //添加一个带箭头的直线            IAutoShape shape4 = slide.Shapes.AppendShape(ShapeType.Line, new RectangleF(660, 200, 100, 100));            shape4.ShapeStyle.LineColor.Color = Color.Red;            shape4.Line.LineEndType = LineEndType.StealthArrow;            shape4.Rotation = -90;//设置形状旋转角度            //添加一个圆形            IAutoShape shape5 = slide.Shapes.AppendShape(ShapeType.Ellipse, new RectangleF(289, 166, 120, 120));            shape5.Fill.FillType = FillFormatType.Solid;            shape5.Fill.SolidColor.Color = Color.White;            shape5.Line.FillType = FillFormatType.Solid;            shape5.Line.SolidFillColor.Color = Color.Red;            //添加一个五角星形状            IAutoShape shape6 = slide.Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(300, 170, 100, 100));            shape6.Fill.FillType = FillFormatType.Solid;            shape6.Fill.SolidColor.Color = Color.Orange;            shape6.Line.FillType = FillFormatType.None;            //设置五角星形状的光边效果            GlowEffect glow = new GlowEffect();            glow.ColorFormat.Color = Color.Yellow;            glow.Radius = 7.0;            shape6.EffectDag.GlowEffect = glow;                        //将shape5和shape6两个形状组合            ArrayList list = new ArrayList();            list.Add(shape5);            list.Add(shape6);            ppt.Slides[0].GroupShapes(list);            //保存文档            ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);            System.Diagnostics.Process.Start("result.pptx");        }    }}
View Code

 

【示例2】将形状保存为图片

步骤1:加载测试文档

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

步骤2:将形状保存为图片

//遍历第一张幻灯片中的所有图形 for (int i = 0; i < ppt.Slides[0].Shapes.Count; i++) {     //获取幻灯片中的图形,并保存为.png格式的图片     Image image = ppt.Slides[0].Shapes.SaveAsImage(i);     image.Save(String.Format("Picture-{0}.png", i), System.Drawing.Imaging.ImageFormat.Png); }

全部代码:

using Spire.Presentation;using System;using System.Drawing;namespace SaveShapesAsImgs_PPT{    class Program    {        static void Main(string[] args)        {            //实例化Presentation类的对象,并加载测试文档            Presentation ppt = new Presentation();            ppt.LoadFromFile("test.pptx");            //遍历第一张幻灯片中的所有图形            for (int i = 0; i < ppt.Slides[0].Shapes.Count; i++)            {                //获取幻灯片中的图形,并保存为.png格式的图片                Image image = ppt.Slides[0].Shapes.SaveAsImage(i);                image.Save(String.Format("Picture-{0}.png", i), System.Drawing.Imaging.ImageFormat.Png);            }        }    }}
View Code

 

(本文完)

转载请注明出处。

你可能感兴趣的文章
计算网速
查看>>
jdk8中HashMap的优化和底层内存的优化
查看>>
js中bind、call、apply函数的用法
查看>>
PHP集群中SESSION共享方案之Redis
查看>>
KVM虚拟化开源高可用方案(三)glusterfs
查看>>
linux中date的用法总结
查看>>
在互联网时代不突破的企业将没有出路
查看>>
linux下新加硬盘
查看>>
Day03 - 挂载、nmcli、yum安装
查看>>
Linux下的qperf测量网络带宽和延迟
查看>>
wxPython 配置环境
查看>>
C的数据类型 关键字
查看>>
Hadoop 2.5.2 HDFS HA+YARN HA 应用配置
查看>>
tomcat远程调试
查看>>
APUE读书笔记-18终端输入输出-05终端选项标记
查看>>
Linux查看系统IO
查看>>
阅后即焚,Python 运维开发99速成
查看>>
Oracle正则表达式(二)
查看>>
oracle导入导出
查看>>
刘宇凡:360搜索来了,百度你怂了吗?
查看>>