经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# 在PPT中绘制形状(shape) - E-iceblue
来源:cnblogs  作者:E-iceblue  时间:2018/11/22 10:26:43  对本文有异议

概述

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

  • 绘制形状

  • 用图片填充形状

  • 在形状中添加文字

  • 设置形状单色、渐变色填充

  • 设置形状阴影效果

  • 组合多个形状为一个

  • 设置形状光边效果

  • 将形状保存为图片

 

工具

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

示例代码(供参考)

【示例1】绘制形状

步骤1:新建一个幻灯片

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

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

  1. ISlide slide = ppt.Slides[0];

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

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

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

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

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

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

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

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

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

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

步骤8:保存文档

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

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

全部代码:

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

View Code

 

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

步骤1:加载测试文档

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

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

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

全部代码:

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

View Code

 

(本文完)

转载请注明出处。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号