C# 在PPT中繪制形狀(shape)


概述

本篇文章將介紹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

 

(本文完)

轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM